douqiaotong8682 2012-08-06 21:11
浏览 102
已采纳

Wordpress函数in_category无法按预期工作

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.

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. When I replace Other Nuts with another category name, like Confections, it works perfectly.

I am assuming this has something to do with the space in the category name Other Nuts. Though, I have tried using it's category ID and category slug to no avail.

Any idea what's going on here??

  • 写回答

2条回答 默认 最新

  • dtxzwdl08169 2012-08-06 21:39
    关注

    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

    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. 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. Wordpress doesn't make the connection between the parent and child category in this case. Being categorized in the child doesn't also categorize it in the parent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起