在循环外的wordpress“in_category”解决方法的Eval字符串

时间:2012-11-17 19:29:15

标签: wordpress eval categories evaluate

在Wordpress中你不能在循环之外使用“in_category”,所以我创建了一个函数,它给了我文章所有的所有类别,并创建了一个“is_category”if语句。

我把我的功能放在我的“functions.php”中:

function inCatAlt($catID){
    $allCats = get_categories('child_of='.$catID);

    $childCats = 'is_category('.$catID.') ';
    foreach($allCats as $childCat){
        $childCats.= 'or is_category('.$childCat->cat_ID.') ';
    };

    $allchildCats = trim(trim($childCats, 'or '));
    return $allchildCats;
}

并在我的侧边栏中单击,依此类推:

echo inCatAlt(13);

这给了我一个回字符串:

"is_category(13) or is_category(16) or is_category(15)"

这正是我所需要的,但现在我想要评估字符串以在这样的if函数中使用它:

if(eval(inCatAlt(13))){
 do something
}

但它不起作用。 我评估错误或问题是什么? 如果我将输出粘贴到if函数中,它可以正常工作......

1 个答案:

答案 0 :(得分:0)

我会回答我认为真正的问题。你提出的解决方案非常糟糕。

试试这个:

$post_categories = wp_get_post_categories( $post_id );
$child_categories = get_categories( array( 'child_of' => $catID ) );

if( count( array_intersect( $post_categories, $child_categories ) ) > 0 )
    // The post exists in one or more of the child categories