lowercase lowercase - 2 months ago 32
PHP Question

Exclude Wordpress Category from get_the_category_list

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

Answer

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;
}

Cheers, the Giphy way!