• Resolved Anders Norén

    (@anlino)


    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • MichaelH

    (@michaelh)

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

    Thread Starter Anders Norén

    (@anlino)

    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.

    MichaelH

    (@michaelh)

    if (in_array($category->cat_name,array('cat1_to_include','cat2_to_include','cat3_to_include')) ) {
    Thread Starter Anders Norén

    (@anlino)

    Thanks, that did it!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Include only specific categories in the_category’ is closed to new replies.