-1

I have a news system with a thumbnail and a box on it that show the category related to the news, using <?php the_category(', '); ?> This worked fine using only one category for each news, but since I downloaded SEO by Yoast, I added a breadcrumb at the top of my page.php and single.php and did a hierarchy for my news.

So now, since I have a hierarchy type like this "Home > News > Sub-category > Sub-sub category".

What should I add instead of the_category to only show the "sub-sub-category" in the news box ? Is this possible ?

EDIT :

Example 1 (before) calling the category with the post set under only 1 category :

html

<div class="news-box">
<div class="category-box">
   <h2><ul class="post-categories">
<li><a href="http://#.fr/category/News/" rel="tag">News</a></li></ul></h2>

    <!--Called using <?php the_category(', '); ?> when only one category is set-->

</div>
</div>

css

.news-box {
background-color:#777;
height:200px;
position:relative;
}
.category-box {
background-color:#FFFFFF;
position:absolute;
bottom:10px;
left:10px;
text-transform:uppercase;
}

https://jsfiddle.net/rpoyxvp9/

Which worked fine so far

Example 2 (now) calling the category with the post set under multiple sub-categories :

html

<div class="news-box">
<div class="category-box">
    <h2><ul class="post-categories">
<li><a href="http://#.fr/category/news/anime/" rel="tag">Anime</a></li>
<li><a href="http://#.fr/category/news/" rel="tag">News</a></li></ul></h2>

    <!--Called using <?php the_category(', '); ?> when multiple categories are set-->

</div>
</div>

css

.news-box {
background-color:#777;
height:200px;
position:relative;
}
.category-box {
background-color:#FFFFFF;
position:absolute;
bottom:10px;
left:10px;
text-transform:uppercase;
}

https://jsfiddle.net/gthsa5mx/

Which doesn't work the way I want, as you can see when multiple categories are set under sub-categories, it shows all the path. But in my example 2, I would like only the sub-category "Anime" to appear on the category-box.

Also, I want it to be called without the <li>, so you're going to tell me to remove the <ul> and <li> but I can't since it is automatically called when using the_category(', ');.

  • Can you give an example with associated example categories, including what you would expect, and what you actually get instead? Be very descriptive. Also edit your question and include your code – Tom J Nowell Jul 11 '15 at 21:38
  • Hi Tom, I just edited my first post, I hope you got the informations you wanted. – dbsso Jul 12 '15 at 10:39
  • Please post your code inside your question. It is really frustrating having to open multiple browser windows to read one post. Also, if those fiddles get deleted, your context is broken – Pieter Goosen Jul 12 '15 at 11:04
  • Sorry I'm usually asked to provide a jsfiddle to make it clearer to see on SO, here you go for the code. I kept the jsfiddle in case someone want a visual aspect. Thanks for your advice though. – dbsso Jul 12 '15 at 12:08
  • Can you post the PHP code? jsfiddles might work for visual things, but this isn't a styling problem. I'm still not clear what the actual problem is, or what you want instead to happen. Can you provide an example scenario that states outright the name of a post, the names of the categories it's in, what you expect to happen, and what actually happens, along with the PHP code that generates it? – Tom J Nowell Jul 12 '15 at 22:16
1

You can make use of the get_the_categories filter to remove the parent categories from the list. the_category() uses get_the_category_list() which in turn uses get_the_category()

The idea is to check the categories returned against an array of parent ids and then removing those categories from the list.

You can try the following (Requires PHP 5.3+)

add_filter( 'get_the_categories', function ( $categories )
{
    /**
     * Count the amount of categories attached to the post
     * If there is only one category, return that category
     */
    if ( count( $categories ) == 1 )
        return $categories;

    /**
     * Get an array of parent terms to test against the terms
     *
     * @see wp_list_pluck()
     * @link https://codex.wordpress.org/Function_Reference/wp_list_pluck
     */
    $parents = wp_list_pluck( $categories, 'parent' );
    foreach ( $categories as $key => $category ) {
        /**
         * Test the category ID against $parents array
         * If the category ID is the $parents array, unset it from the
         * $category array
         */
        if ( in_array( $category->term_id, $parents ) )
            unset( $categories[$key] );
    }

    // Return new $categories array
    return $categories;
});

EDIT

the_category( ', ' ); should return a list of categories separated by a comma. If only one category exists, it should display that category without any bullet points

  • Based on my testing, this code removes the parent category when the post has been assigned to both the parent and child category. – Brad Dalton Jul 12 '15 at 19:32
  • 2
    @BradDalton yes, that is the intention of the code, remove all parents and only show the lowest child ;-) – Pieter Goosen Jul 13 '15 at 3:58
  • It's essentially my answer, except it's code that automatically unticks the box for you. A category contains all the posts inside it, and that includes all its subcategories. Think of it as a box inside another box, rather than children and parents – Tom J Nowell Jul 13 '15 at 18:49
1

Your problem is that you've misunderstood how categories work.

To demonstrate this, I'm going to use a hypothetical taxonomy called "colour":

Alice creates a post called 'box', and gives it the category "pale red". The pale red category is a sub category of a top level category named "Red", such that Pale Red is a child of Red.

Alice shows Bob the post named 'box', and Bob sees that it has the category 'Pale Red'.

Alice asks Bob:

"Is 'box' in the category 'Red'?"

The correct answer is yes. But the way you've used WordPress suggests that you think the answer is no. Categories are inclusive, hierarchical. A parent category contains its child categories, and that includes any posts in those child categories.

Based on your question, it looks like you've put the post in the 'Animé' category, but you've also explicitly put it in the 'News' category by checking both boxes. This is unnecessary, you only need to check the Animé box.

Putting it in the news category is redundant, and causes the problem you're having, in the same way that Alice telling Bob that Pale Red posts are also Red posts would be patronising.

So instead, tick only the Animé category. WordPress understands that posts inside a subcategory belong to their parent categories too.

  • This would be the solution yes. My solution just removes the parent if the OP decides to stick with his current method ;-) – Pieter Goosen Jul 13 '15 at 18:29

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.