WordPress.org

Ready to get started?Download WordPress

Forums

If Catagories > 3 don't display code... (2 posts)

  1. yellowputty
    Member
    Posted 2 years ago #

    Hello WordPress & Coding Gurus,

    When I post an item in my blog, I have the catagories displayed below the post title. For reasons I won't go into, I sometimes may have up to 100 catagories selected for an individual post.

    In the cases where there are 3 or more (say) catagories for a post, I don't want it to display all of them.

    Ugh, that sounds messy! If you followed, is such a thing possible?

    Many thanks.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    to limit the category output to three,
    try to use a filter function, added to functions.php of your theme:

    example:

    add_filter('the_category','the_category_max_output');
    // limit output to max 3 categories in the loop
    function the_category_max_output($output) {
    	if( in_the_loop() ) :
    	$list = explode('</a>',$output); $output = '';
    	for( $i; $i < min(3,count( $list )); $i++ ) {
    	$output .= array_shift($list).'</a>'; }
    	endif;
      return $output;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic