• For each posting in my blog, I want to display a list of its categories, including links to those categories. In other words, something like this:
    My Post Title. Posted in: Category 1, category 2, category 3

    My problem is that I only want to show the first 5 categories each post is posted in (because I don’t want the list to get super long).

    Is there a way to do that?

Viewing 1 replies (of 1 total)
  • The category list is usually generated by the statement:

    <?php the_category(', '); ?>

    Replace that statement with this:

    <?php
    $count = 0;
    $limit = 5;
    $sep = '';
    foreach((get_the_category()) as $category) {
       if (++%count > $limit) break;
       $cat_name = $category->cat_name;
       $cat_link = get_category_link( $category->cat_ID );
       echo "<a href='$cat_link' title='$cat_name'>$sep$cat_name</a>";
       $sep = ', ';
    }
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Displaying post categories’ is closed to new replies.