coops
coops

Reputation: 1714

Wordpress / PHP: check if post is in only in one category

Basically, I want to hide the category list if the author hasn't chosen a category for the post yet (so that it doesn't show 'uncategorised' on the archive page).

I need to check if the post is ONLY in the 'uncategorised' category in Wordpress. I am checking if the post is in this category with:

<?php if (!in_category( 'uncategorised' )) { ?>
    <div class="category">
        <?php the_category(', '); ?>
    </div>
<?php } ?>

Which works great - however any post that is in 'uncategorised' AND another category is also not appearing. So, i need to also check if that is the ONLY category it is in. So something like '&& is only in one category' is what i'm looking for, but my google searches have not come up with any solutions.

Please could someone point me in the right direction with this?

Upvotes: 0

Views: 1037

Answers (1)

Jaaqo
Jaaqo

Reputation: 66

There is a built in function that could help you here:

https://codex.wordpress.org/Function_Reference/get_the_category

Example of ”only one category”:

$categories = get_the_category()

if ( count($categories) == 1 ) { ... }

Upvotes: 2

Related Questions