Last active
February 26, 2021 21:11
-
-
Save tomjn/7faaca9b78310f51c6bbe2391af0337b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function exclude_post_categories( $exclude = '', $spacer = ' ' ) { | |
$categories = get_the_category(); | |
if ( empty( $categories ) ) { | |
return; | |
} | |
$exclude = explode( ',', $exclude ); | |
$counter = count( get_the_category() ) - count( $exclude ); | |
foreach ( $categories as $cat ) { | |
$counter--; | |
if ( in_array( $cat->cat_ID, $exclude, true ) ) { | |
continue; | |
} | |
?> | |
<a | |
href="<?php echo esc_url( get_category_link( $cat->cat_ID ) ); ?>" | |
title="<?php echo esc_attr( $cat->cat_name ); ?>" | |
> | |
<?php echo esc_html( $cat->cat_name ); ?> | |
</a> | |
<?php | |
if ( 0 === $counter ) { | |
echo $spacer; | |
} | |
} | |
} |
lowhanging fruit, wp_parse_str
or wp_parse_args
could allow the exclusion parameter to be both "1,2,3"
or [1,2,3]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken and improved from https://css-tricks.com/snippets/wordpress/the_category-excludes/:
$post
was used but never declared