简体   繁体   English

Wordpress函数in_category无法正常工作

[英]Wordpress function in_category not working as expected

This is driving me crazy, and I have tried a variety of different things. 这让我发疯,并且我尝试了各种不同的方法。 Essentially, the desired effect is to target two different categories in Wordpress using the built in in_category function. 本质上,所需的效果是使用内置的in_category函数将Wordpress中的两个不同类别作为目标。

Here is my code as it stands currently: 这是我目前的代码:

if(in_category( array("Snacks", "Other Nuts") )) :
 //do something
endif;

This will work with the category Snacks but not with the category Other Nuts . 这将与“ Snacks ”类别一起使用,而不与“ Other Nuts ”类别一起使用。 When I replace Other Nuts with another category name, like Confections , it works perfectly. 当我用另一个类别名称(例如Confections替换Other Nuts时,它可以很好地工作。

I am assuming this has something to do with the space in the category name Other Nuts . 我假设这与类别名称Other Nuts的空间有关。 Though, I have tried using it's category ID and category slug to no avail. 虽然,我尝试使用它的类别ID和类别标签无效。

Any idea what's going on here?? 知道这里发生了什么吗?

Figured it out. 弄清楚了。

Let's say you have two categories, one is a parent of the other, like so: 假设您有两个类别,一个类别是另一个类别的父类别,如下所示:

Other Nuts (Parent)
    Almonds (Child)

If you make a post in Wordpress and categorize it in Almonds and run a simple loop like 如果您在Wordpress中发布帖子并在Almonds对其进行分类,然后运行一个简单的循环,例如

if(have_posts()) :
  while(have_posts()) : the_post();

  // run your loop

  endwhile;
endif;

You would get the output of the Almonds post that belongs to the Other Nuts parent category that's categorized in Almonds . 你会得到的输出Almonds属于后Other Nuts是在分类的父类Almonds Now, if you were to run this loop instead: 现在,如果要运行此循环,请执行以下操作:

if(have_posts()) :
  while(have_posts()) : the_post();

    if(in_category('Other Nuts')) :  

       // run your loop

    endif;

  endwhile;
endif;

You would get nothing. 你什么也得不到。 The reason is because you have only categorized the post in Almonds and not also in Other Nuts . 原因是因为您只将帖子分类为Almonds而不是Other Nuts Wordpress doesn't make the connection between the parent and child category in this case. 在这种情况下,WordPress不会在父类别和子类别之间建立连接。 Being categorized in the child doesn't also categorize it in the parent. 在孩子中进行分类也不会在父母中进行分类。

Essentially, this should check all current category IDs for the post against all of the ID's that you're expecting, then check all of the parent category IDs against what you're expecting. 本质上,这应该根据您期望的所有ID检查该帖子的所有当前类别ID,然后根据您的期望检查所有父类别ID。 You could compare the category names, instead, with a slight variation to this code. 您可以对类别名称进行比较,但与此代码略有不同。

Step 1: Put this in your functions.php file: 步骤1:将其放入您的functions.php文件中:

function check_category_family( $categories, $expected_ids ){
  foreach( $categories as $i ){
    if( in_array( intval( $i->category_parent ), $expected_ids ) ){
      return true;
    }
  }
}

Step 2: Put this psuedo-code into whatever category template(s) you're building: 第2步:将此伪代码放入您要构建的任何类别模板中:

$categories = get_the_category();
$expected_ids = array( /*PUT YOUR CATEGORY IDS AS INTEGERS IN HERE*/ );

if( in_category( $expected_ids ) || check_category_family( $categories, $expected_ids ) ){
  //run the loop
} else {
  //redirect?
}

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

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