Forums

[resolved] Include only specific categories in the_category (5 posts)

  1. Anders
    Member
    Posted 2 years ago #

    Hi!

    Currently, every post on my blog has a postmeta-area above it, displaying the number of comments and the categories that the posted has been posted in using the_categories. You can see it in action here.

    Obviously, this gets pretty messy when a post is included in a lot of categories. I was wondering whether it is possible to make sure that only a specific group of categories can be displayed above the post. For example, if a post is posted in "Filmer", "Electronic Arts" and "Nintendo DS", only "Nintendo DS" would appear.

    Thanks in advance!

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    Exclude a category displaying under posts:

    <?php
    foreach((get_the_category()) as $category) {
        if (!in_array($category->cat_name,array('cat1_to_exclude','cat2_to_exclude','cat3_to_exclude')) ) {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> ';
    }
    }
    ?>
  3. Anders
    Member
    Posted 2 years ago #

    Thanks! However, is there any way to change the code so I can specify what categories to include, as opposed to what categories to exclude? Seeing how my blog has a grand total of 67 categories, of which I would want to exclude 64.

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    if (in_array($category->cat_name,array('cat1_to_include','cat2_to_include','cat3_to_include')) ) {
  5. Anders
    Member
    Posted 2 years ago #

    Thanks, that did it!

Topic Closed

This topic has been closed to new replies.

About this Topic