【发布时间】:2016-03-18 22:06:26
【问题描述】:
我真的是 php 的新手,我正在设计一个 wordpress 主题。我正在尝试为带有子菜单项的 foreach 自定义菜单上的活动项添加一个类:
<?php
foreach ($taxonomies as $taxonomy ) {
$children = get_term_children($taxonomy->term_id, $taxonomy_name);
$args = array(
'hide_empty' => true,
'order_by' => 'name',
'parent' => $taxonomy->term_id
);
$children = get_terms($taxonomy_name, $args);
?>
<ul>
<li class="<?php echo (in_array($taxonomy->term_id, $active_terms) ? 'active' : '' )?>"><a href="<?php echo get_term_link($taxonomy->term_id, $taxonomy_name ) ?>"><?php echo $taxonomy->name ?></a></li>
<?php if(!empty($children)) : ?>
<ul class="<?php echo (in_array($taxonomy->term_id, $active_terms) ? 'active' : '' ) ?>" >
<?php foreach($children as $child) : ?>
<li class="<?php echo (in_array($child->term_id, $active_terms) ? 'active' : '' )?>"><a href="<?php echo get_term_link($child->term_id, $taxonomy_name ) ?>"><?php echo $child->name ?></a></li>
<?php endforeach; ?>
</ul>
<?php endif;?>
<?php
}?>
</ul>
<?php
}
?>
工作正常,但是当我在 <ul> $taxonomy 并且没有 <li> 必须处于活动状态时,它还会将“活动”类添加到数组中的第一个 <li> 子项。我做错了什么?我怎么能解决这个问题?我一直在寻找,但我对 php 和 wordpress 主题真的很陌生,感谢任何帮助!
非常感谢!
【问题讨论】:
-
所以你的问题在
- 里面的大
- 里面没有孩子..对吧?
- 里面的大
-
问题出在内部
- 内的
- 中,当我在显示数组中所有项目的
- 页面中时,
- 是第一个数组上的项目它变为“活动”,当它不是因为我不在那个页面中时。我希望我解释得很好。
- 中,当我在显示数组中所有项目的