0

I'm trying to limit the amount of categories that show up above my post title, below is an example of what is happening enter image description here I'm fine with just grabbing the first 2 categories that show up and limit them to that.

I have found functions that accomplish this with tags but nothing with categories. For example in this custom function, it limits the tags to five in functions.php

add_filter('term_links-post_tag','limit_to_five_tags');
function limit_to_five_tags($terms) {
return array_slice($terms,0,5,true);
}

Then in single.php use <?php the_tags() ?>.

Any idea how to accomplish this with categories?

1 Answer 1

0

In your functions.php, you can filter the_category_list, which is an array of categories for a post.

function wpse_the_category_list( $categories, $post_id ) {
  return array_slice( $categories, 0, 2, true );
}
add_filter( 'the_category_list', 'wpse_the_category_list', 10, 2 );

And then display the categories in your template file with:

the_category();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.