如何检查当前cat是否在数组内并且IF是否为父类

时间:2016-10-04 11:44:51

标签: php wordpress categories

我有一个名为$best_cats的全局

global $best_cats;
$best_cats =  array(8,31,51,102,217,218);

现在我需要检查当前cat是否在数组内,并且IF当前cat是否是父猫。

我尝试这种方式但是没有用。我做错了什么,我该怎么做?

<?php 
    $this_category = get_category($cat);
    $cat_id = $this_category->cat_ID;
    if (in_array($cat_id, $best_cats) && ($this_category->category_parent == 0 )) { ?>

    //category is inside of the array and is a parent cat

    <?php } else { ?>

    //category is not inside the array and It's not a parent either

    <?php } ?>

1 个答案:

答案 0 :(得分:2)

您似乎忘记了global

 <?php 
        global $best_cats;
        $this_category = get_category($cat);
        $cat_id = $this_category->cat_ID;
        if (in_array($cat_id, $best_cats) && ($this_category->category_parent == 0 )) {
            //category is inside of the array and is a parent cat
        } else {    
            //category is not inside the array and It's not a parent either
        }