简体   繁体   English

Category_slug类别儿童wordpress

[英]Category_slug of category children wordpress

I have a category "Decouverte" witch has three subcategories : "actualité", "vidéo" and "coup de coeur". 我有一个“ Decouverte”女巫类别,它有三个子类别:“actualité”,“vidéo”和“ coup de coeur”。

When I sort the posts according to their subcategories, everything works except for the category "video"; 当我按照帖子的子类别对帖子进行排序时,除“视频”类别外,其他所有内容都有效; the category_slug displays "decouverte". category_slug显示“ decouverte”。

On my dashboard, I see this : 在我的仪表板上,我看到了: 我的帖子

The subcategory "Video" is displayed last, maybe it's why it doesn't work ? 子类别“视频”显示在最后,也许是为什么它不起作用?

My code : 我的代码:

        $categories = get_the_category(); 
        $category_slug = $categories[0]->slug;

        echo $category_slug;

       if ( $category_slug  == 'actualite' ) { ?>
          <div class="picto_home icone_actualite"></div><?php
        } 

        elseif ( $category_slug  == 'video' ) { ?>
          <div class="picto_home icone_video"></div><?php
        } 

        elseif ( $category_slug  == 'coupdecoeur' ) { ?>
          <div class="picto_home icone_coupdecoeur"></div><?php
        } 

        else { echo "Doesn't work"; } ?>

And the website : http://www.overso.me/ It's on the right block 和网站: http : //www.overso.me/它在右侧

Problem source 问题来源

It's because you have got two categories applied to all posts. 这是因为您对所有帖子都应用了两个类别。

Categories in each post are sotrted alphabetically. 每个帖子中的类别均按字母顺序排序。

In your case you take $category_slug = $categories[0]->slug; 在您的情况下,您将$category_slug = $categories[0]->slug; , which means: , 意思是:

Take first category slug name from categories array 从类别数组中获取第一个类别子弹名称

For each post it is: 对于每个帖子,它是:

  1. $categories[0] = Decouverte , $categories[1] = Video because D < V $categories[0] = Decouverte$categories[1] = Video因为D < V
  2. $categories[0] = Actualite , $categories[1] = Decouverte because A < D $categories[0] = Actualite$categories[1] = Decouverte因为A < D
  3. $categories[0] = Coup de coeur , $categories[1] = Decouverte because C < D $categories[0] = Coup de coeur$categories[1] = Decouverte因为C < D

How to make it work as you want 如何使它如你所愿

Ok so here is my approach for your problem. 好的,这是我为您解决问题的方法。

Before checking for $categories[0]->slug , lets check is $categories[0] object a PARENT category. 在检查$categories[0]->slug ,先检查一下$categories[0]对象是否为PARENT类别。 You can do this by checking: 您可以通过检查以下内容来做到这一点:

$categories = get_the_category();

// Remove parent category object from array if it's on first place
if($categories[0]->parent == 0){
    unset($categories[0]);
    $categories = array_values($categories);
}

As you see if $categories[0] is a parent category ( if($categories[0]->parent == 0) ), then remove this result from array $categories - unset($categories[0]) . 如您所见,如果$categories[0]是父类别( if($categories[0]->parent == 0) ),则从数组$categories - unset($categories[0])删除此结果。

Now you can just reset indexing of this array to starting from 0 , by calling $categories = array_values($categories) . 现在,您可以通过调用$categories = array_values($categories)将此数组的索引重置为从0开始。

From now, you can easly call $categories[0]->slug , because there is no parent category on first place for sure. 从现在开始,您可以轻松地调用$categories[0]->slug ,因为肯定没有父类别。

Full code: 完整代码:

$categories = get_the_category();

// Remove parent category object from array if it's on first place
if($categories[0]->parent == 0){
    unset($categories[0]);
    $categories = array_values($categories);
}

$category_slug = $categories[0]->slug;

if ( $category_slug  == 'child-1' ) {
   echo "1111";
} 

elseif ( $category_slug  == 'child-2' ) {
   echo "2222";
} 

elseif ( $category_slug  == 'child-3' ) {
   echo "3333";
} 

else { echo "Doesn't work"; }

Of course change if statements logic to your data. 当然,如果语句逻辑对您的数据进行更改。

More general solution for future reference 更通用的解决方案,以供将来参考

Ok, here is some general solution I wrote. 好的,这是我写的一些通用解决方案。

If you need to get only subcategories for any post, for any number of categories applied to the post, and no matter on which place parent category will be in the categories array, you should try this. 如果您只需要获取任何帖子的子类别,应用于该帖子的任意数量的类别,并且无论父类别位于类别数组中的哪个位置,都应尝试此操作。

First, add this function to your functions.php file (or to your plugin file). 首先,将此函数添加到您的functions.php文件(或您的插件文件)中。

function du_get_post_subcategories( $post_id ){
    $categories = get_the_category( $post_id );

    $i = 0;
    foreach ($categories as $category) {
        if($category->parent == 0){
            unset($categories[$i]);
        }
        $i++;
    }

    $categories = array_values($categories);
    return $categories;
}

And then inside your loop you can call 然后在循环中您可以调用

$subcategories = du_get_post_subcategories( $post->ID );

If you have post ID, you can call this outside the loop as well 如果您有帖子ID,也可以在循环外部调用它

$subcategories = du_get_post_subcategories( $your_post_id );

This function will return all subcategories in the same format as get_the_category() function. 此函数将以与get_the_category()函数相同的格式返回所有子类别。

If you provide array with only one, parent category. 如果您仅提供一个父类别的数组。 Function will return an empty array. 函数将返回一个空数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM