在cat_is_ancestor_of中将数组用作$ cat1

时间:2014-09-08 15:26:28

标签: arrays wordpress categories

我的category.php使用wp_list_categories显示某些类别的子类别,并显示这些子类别的兄弟类别。特定子类别在以下字符串中定义:

if ($this_category && ((cat_is_ancestor_of(258, $cat) or is_category(258)) || (cat_is_ancestor_of(252, $cat) or is_category(252)) || (cat_is_ancestor_of(238, $cat) or is_category(238)) ) ) { ?>  

我希望将此替换为数组,以便在相关类别发生变化时更易于维护。我写了以下内容:

<?php $subCat = array(258, 252, 238);?>
if ($this_category && (cat_is_ancestor_of($subCat, $cat) or is_category($subCat) ) ) { ?> 

这适用于父类别页面,但不适用于子类别页面。在子类别中,兄弟姐妹没有显示。

cat_is_ancestor_of可以为第一个变量使用数组吗?

1 个答案:

答案 0 :(得分:0)

你应该遍历你的数组。试试这个:

<?php 
$subCat = array(258, 252, 238);
$sub_str    = array();
foreach($subCat as $sub){
$sub_str[]    = '(cat_is_ancestor_of($sub, $cat) or is_category($sub))';
}
$qry    = implode($sub_str,'||');
if ($this_category && ($qry) ) { 
// do something
}
?>