我的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可以对第一个变量使用数组吗?
发布于 2014-09-08 23:37:02
你应该遍历你的数组。试试这个:
<?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
}
?> https://stackoverflow.com/questions/25727724
复制相似问题