WordPress.org

Support

Support » How-To and Troubleshooting » How to choose which category displays when more than one

How to choose which category displays when more than one

  • Jenny Beaumont

    @jennybeaumont

    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.

    help?

    many thanks,
    -jennyb

Viewing 3 replies - 1 through 3 (of 3 total)
  • Jenny Beaumont

    @jennybeaumont

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

    Jenny Beaumont

    @jennybeaumont

    BAM, nailed it:

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

    Jenny Beaumont

    @jennybeaumont

    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.

    cheers,
    -jennyb

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to choose which category displays when more than one’ is closed to new replies.