• Resolved Nathan Hawkes

    (@natehawkes)


    Hi,

    I’m posting this because my brain has turned to mush right now, and I am not able to focus. Apologies if this has been answered already, but the answers I have found have varied in different ways.

    I want to have a paragraph that displays my categories. Obviously, all posts are considered to have a category attached to it, thanks to the “Uncategorized” category. The code I have at the moment is:

    <p>Posted under: <?php the_category(', '); ?></p>

    I know that’s the code for “Display all categories”. Is it possible to have “Uncategorized” filtered from this list and then, if there are no categorized in the list after this, to not display the paragraph at all as it would be empty, save for the words “Posted under:”? So, a post with categories of “Test” and “test two” will show:

    <p>Posted under: Test, Test two</p>

    posts with a category of just “Test” will show:

    <p>Posted under: Test</p>

    and posts with only the “Uncategorized” category won’t show the paragraph at all?

    Again, apologies for posting this here considering the answers already available, but my brain is just not working right now.

Viewing 1 replies (of 1 total)
  • Thread Starter Nathan Hawkes

    (@natehawkes)

    After a good few days of wrangling with it, I finally got the solution I wanted. It isn’t pretty or elegant, but I found that this works:

    <?php
              $cats = '';
              foreach ( get_the_category() as $cat ){
                if ( count( get_the_category() ) == 1 && $cat->name == "Uncategorized" ) { $catsl = ''; $catsep = ''; }
                else {
                  $catsl = '<p class="posted-under">Posted in:';
                  $catsep = '</p>';
                  if ( $cat->cat_name != "Uncategorized" ) {
                    $cats .= '<a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( 'View all posts in %s' ), $cat->name ) . '">' . $cat->name . '</a>, ';
                  }
                }
              }
              $cats = substr($cats,0,-2);
              echo $catsl . ' ' .$cats . $catsep;
              ?>

    Note that the Uncategorized category name appears to be cAsE sEnSiTiVe, as "uncategorized" didn’t work with me, but "Uncategorized" did.

Viewing 1 replies (of 1 total)
  • The topic ‘Remove "Uncategorized" from category list’ is closed to new replies.