WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] hide 'uncategorized' in list of post's categories (11 posts)

  1. badjeros
    Member
    Posted 4 years ago #

    I'm using

    <?php the_category('') ?>

    to show the categorys that a given post belongs to in that post's metadata.

    In cases where the post is in the category 'uncategorized' I would like to have no category listed, instead of it being specifically labelled 'uncategorized'.

    Is there a way to hide that default category in a list of categories?

  2. MichaelH
    Member
    Posted 4 years ago #

    Exclude a category displaying under posts:

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

    Perfect! Thanks for the swift solution.

  4. vegasrenie
    Member
    Posted 4 years ago #

    Where would this be inserted? Not a newbie to WP, but still have serious coding issues. :-)

  5. MichaelH
    Member
    Posted 4 years ago #

    With the help of the Template Hierarchy article, determine what Template is displaying the posts you are talking about (could be index.php). Then edit that Template and add the code.

  6. badjeros
    Member
    Posted 4 years ago #

    I know this is stretching things, but I don't suppose there's a way to insert a comma after each categoy, excepting the final one, while still maintaing the Uncategorized filter functionality provided by MichaelH?

  7. alchymyth
    Forum Moderator
    Posted 4 years ago #

    <?php
    $igc=0;
    foreach((get_the_category()) as $category) {
        if ($category->cat_name != 'uncategorized') {
    if($igc != 0) { echo ', '; }; $igc++;
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
    }
    }
    ?>
  8. badjeros
    Member
    Posted 4 years ago #

    That does it very nicely. Thanks for your help alchymyth, I had no chance of figuring that one out on my own.

  9. alby54
    Member
    Posted 3 years ago #

    I do it with CSS only because it is faster.
    Just add:
    .cat-item-1 {display:none;}
    changing the ID number if different.

  10. aw2
    Member
    Posted 3 years ago #

    Hi there,

    I'm using the excellent paragrams theme for my blog.
    I want to hide certain categories from the metadata. I tried hard to do what's described here in the forum, but it doesn't seem to work.

    Here's my code from the theme:

    <div class="postmetadata">
    <?php printf(__('%s'), get_the_category_list(' | ')); ?> ˑ 
    </div>

    Any help is very much appreciated.
    Thanks!

    Arthur

  11. dinoraptor101
    Member
    Posted 3 years ago #

    Guys why don't you just use plugins :S

    Here's a plugin that hides any category from list and menus..
    ( WP Hide Categories )

Topic Closed

This topic has been closed to new replies.

About this Topic