1
votes

I have some code that lists the category of the post you are currently on like so...

<?php $categories_list = get_the_category_list( __( ', ', 'sitename' ) );
if ( $categories_list && sitename_categorized_blog() ) :?>
<span class="cat-links">
<?php printf( __( 'ALL NEWS FOR %1$s', 'sitename' ), $categories_list ); ?>
</span>
<?php endif; // End if categories ?>

This works great, but I just added a new ctegory called 'featured-posts'. How do I exclude this featured-posts category (either by name or ID), from appearing in the menu generated by above code?

So instead of the result...

ALL NEWS FOR: CATEGORY NAME, FEATURED POST

I just get...

ALL NEWS FOR: CATEGORY NAME

3
Use a custom foreach like they show in this thread wordpress.org/support/topic/…Zach

3 Answers

2
votes

Explanation

Hi, use wp_list_categories function instead and use 'exclude' argument. Refer to this codex page for further explanations.

Example

One place where you can put this code is inside your theme's functions.php file. Following you have an example:

function list_categories_without_this_cat() {
  $categories_stripped_of_one = wp_list_categories( array(
    'exclude' => array( 8 )
  ) );
  return $categories_stripped_of_one;
}
0
votes

The problem with using list_categories is that it lists them in a list and not as it does with get_the_category_list using commas to separate.

0
votes

I found a great answer for it here:

https://stackoverflow.com/a/34817401/5614002

Just call get_the_category_list instead of get_the_tags_list.

I re-named the callback function exclude_cats (instead of exclude_terms), added an array of categories I wanted to skip and it was a breeze!