How to choose which category displays when more than one (4 posts)

  1. Jenny Beaumont
    Posted 3 years ago #

    Hi there,

    So I've been using echo $category[0]->cat_name; to display the name of the category next to each post on a variety of pages (home, category, etc). Up til now, each post has only belonged to one category. Recently I added a new category, to which some posts will also belong, but I don't want that category name to appear in the display.

    It seems that WordPress is choosing which category to use by alphabetical order. So when this new category starts with a letter higher up in the alphabet than its original category name, that one is displayed.

    How can control this? Preferably without a plugin...

    I tried $category[1]->cat_name;, but that only works if the post has at least 2 categories, otherwise it shows blank.


    many thanks,

  2. Jenny Beaumont
    Posted 3 years ago #

    maybe this is an advanced topic? should I post in WP advanced?

  3. Jenny Beaumont
    Posted 3 years ago #

    BAM, nailed it:

    <?php if ($category[1]!='') { echo $category[1]->cat_name; } else { echo $category[0]->cat_name; }?>

  4. Jenny Beaumont
    Posted 3 years ago #

    Damn. Totally didn't nail it. That simply returns whichever cat is lower in the alphabet. Grrrr.

    Still could use some help if anyone has ideas. Trying to either sort by ID, or exclude child categories...either would work for my needs.


Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.