【问题标题】:I want to get 2 specific categories in wp with get_categories method我想用 get_categories 方法在 wp 中获得 2 个特定类别
【发布时间】:2019-10-03 06:29:57
【问题描述】:

我尝试使用get_categories 方法来解决这个问题,但我得到了 整个类别列表。
我只想要2个类别。

我的代码:

$get_parent_cats = array(
        'parent' => '0',
        'hide_empty'=> 0,   //get top level categories only
        'exclude' =>1
    ); 

 get_categories( array( 'category' => array(31,32)));

 $all_categories = get_category(31);//get parent categories  

【问题讨论】:

    标签: php wordpress filtering


    【解决方案1】:

    您可以像这样使用 get_terms 函数根据您的要求获取类别

    $terms= get_terms( array(
        'taxonomy' => 'category',
        'include' => array('31', '32'),
        'hide_empty' => false,
    ) );
    

    【讨论】:

    • 它的工作很棒我又做了一件事,将 31,32 放入一个数组中,然后使用 foreach 和 get_category($array);这也有效。但是您提供了正确的方法。非常感谢
    • $ar=array(33,34,31,32); $nn=数组(); foreach($ar as $a) { //返回 $a; $all_categories = get_category($a); $nn[]=$all_categories; } 返回 $nn;
    • 这样我做了jaimin。现在这不是正确的做法。
    • 在上面的 $terms 数组中,您已经获得了类别数组,因此您不必在其中使用 get_category
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多