pop*_*g22 2 php wordpress highlight categories
我已经使用wp_list_categories来创建类别列表并设置$ args如下所示,将类"current-cat"添加到当前类别项目,一切正常但是当我点击"所有类别"时我无法突出显示列表菜单,因为该类"current-cat"不适用于"所有类别"项目.
如何将当前cat类应用于"所有类别"?
我的设定
<ul>
<?php
$args = array(
'show_option_all' => 'All Categories',
'orderby' => 'id',
'style' => 'list',
'use_desc_for_title' => 0,
'hierarchical' => 0,
'title_li' => '',
'current_category' => 0
);
wp_list_categories( $args );
?>
</ul>
Run Code Online (Sandbox Code Playgroud)
HTML输出
<ul> <li class="cat-item-all"><a href="http://example.com/">All Categories</a></li> <li class="cat-item cat-item-1 current-cat"><a href="http://example.com/category/category-one/">Category one</a></li> <li class="cat-item cat-item-2"><a href="http://example.com/category/category-two/">Category two</a></li> <li class="cat-item cat-item-3"><a href="http://example.com/category/category-three/">Category three</a></li> <li class="cat-item cat-item-4"><a href="http://example.com/category/category-four/">Category four</a></li> </ul>
如果您不回显结果而是将它们存储在变量中,我们可以检查该类是否存在.如果没有,则意味着我们处于"全部"类别.
所以要做到这一点,我做了以下几点:
$args = array(
'show_option_all' => 'All',
'title_li' => '',
'echo' => false, // dont echo the results
'taxonomy' => 'tribe_events_cat'
);
$categories = wp_list_categories($args); // store the results in a variable
if(strpos($categories,'current-cat') == false) { // check if the class exists
// add the class to the All item if it doesn't exist
$categories = str_replace('cat-item-all', 'cat-item-all current-cat', $categories);
}
echo $categories;
Run Code Online (Sandbox Code Playgroud)
您必须更改$ args以适合您的目的.