0

Can someone kindly explain the reason for the following problem?

Let's assume you create a category like "sample" for your posts. While editing a post like "Hello World" in Wordpress, you choose both "Uncategorized" and the newly created category. Then in a theme like "Twenty Fifteen", you see both category names under the mentioned post in the homepage. Now, if you uncheck the "uncategorized" category in the post, the name of the "sample" category does not appear under the post either. It is like they are dependent on each other which is not logical.

2
  • 1
    No, that's not logical. Are you sure 'sampled' is still checked in the post? have you cleared your browser's cache?
    – cjbj
    Jul 6 '16 at 7:33
  • @cjbj. Yes, I have cleared the cache.
    – developer
    Jul 7 '16 at 15:12
0

If you look at the twentyfifteen theme (and the other bundled themes) you'll find code similar to the following: (this is taken from the twenty fifteen theme from inc/template-tags.php)

$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
if ( $categories_list && twentyfifteen_categorized_blog() ) {
    printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
        _x( 'Categories', 'Used before category names.', 'twentyfifteen' ),
        $categories_list
    );
}

Obviously, you have a value for $category_list, so just based on this, your list should display regardless of how many categories is assigned to a post.

The curveball lies in the && twentyfifteen_categorized_blog() condition of your if condition. It is here where you see that your conditional statement fails. But first, lets look at the twentyfifteen_categorized_blog() functions (as I said, the other bundled themes has got similar functions)

function twentyfifteen_categorized_blog() {
    if ( false === ( $all_the_cool_cats = get_transient( 'twentyfifteen_categories' ) ) ) {
        // Create an array of all the categories that are attached to posts.
        $all_the_cool_cats = get_categories( array(
            'fields'     => 'ids',
            'hide_empty' => 1,
            // We only need to know if there is more than one category.
            'number'     => 2,
        ) );
        // Count the number of categories that are attached to the posts.
        $all_the_cool_cats = count( $all_the_cool_cats );
        set_transient( 'twentyfifteen_categories', $all_the_cool_cats );
    }
    if ( $all_the_cool_cats > 1 ) {
        // This blog has more than 1 category so twentyfifteen_categorized_blog should return true.
        return true;
    } else {
        // This blog has only 1 category so twentyfifteen_categorized_blog should return false.
        return false;
    }
}

What this function does is to count how many categories are there on your site, but it only counts the amount of categories which actually have posts assigned to them.

In your example, with only the default post Hello World (having no other posts at all), if you assign only the uncategorized category or only the sample category to that post, it means just one of those categories has a post assigned to it, the other one will be empty. Because we are hiding empty categories in get_categories() inside the twentyfifteen_categorized_blog() function, get_categories() will only return one category, the one with the post assigned to it.

Now, if you look at the twentyfifteen_categorized_blog() function, if the total amount of categories returned by get_categories is zero or 1, the function will return false, and this fails your condition which in turn is why you do not see the category being displayed assigned to your post.

If you assigned both categories to the post, get_categories will return 2 categories (the maximum amount which will be returned at any time), because both categories has a post assigned to it, which will let the function return true, which will fire and display your condition.

As you create more and more categories and assign posts to them, get_categories() inside the twentyfifteen_categorized_blog() will always return 2 cateories (because we only query 2 at most) which will always let twentyfifteen_categorized_blog() return true which will always fire your code for as long as a post have at least one category assigned to it

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.