jQuery解决方案:
如果要使用jQuery,可以尝试以下方法:
<script>
jQuery(document).ready(function($) {
$(\'li.cat-item:has(ul.children)\').addClass(\'i-have-kids\');
});
</script>
添加类的步骤
i-have-kids
致所有
li
包含项目的父级
ul.children
, 在从生成的HTML中
wp_list_categories()
.
Category walker解决方案:
您可以查看
Walker_Category
中的类
/wp-includes/category-template.php
并用一个额外的部分进行扩展,如:
$termchildren = get_term_children( $category->term_id, $category->taxonomy );
if(count($termchildren)>0){
$class .= \' i-have-kids\';
}
如果跳过馈送图像和馈送部分,扩展的walker可能会如下所示:
class Walker_Category_Find_Parents extends Walker_Category {
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( \'list_cats\', $cat_name, $category );
$link = \'<a href="\' . esc_url( get_term_link($category) ) . \'" \';
if ( $use_desc_for_title == 0 || empty($category->description) )
$link .= \'title="\' . esc_attr( sprintf(__( \'View all posts filed under %s\' ), $cat_name) ) . \'"\';
else
$link .= \'title="\' . esc_attr( strip_tags( apply_filters( \'category_description\', $category->description, $category ) ) ) . \'"\';
$link .= \'>\';
$link .= $cat_name . \'</a>\';
if ( !empty($show_count) )
$link .= \' (\' . intval($category->count) . \')\';
if ( \'list\' == $args[\'style\'] ) {
$output .= "\\t<li";
$class = \'cat-item cat-item-\' . $category->term_id;
$termchildren = get_term_children( $category->term_id, $category->taxonomy );
if(count($termchildren)>0){
$class .= \' i-have-kids\';
}
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= \' current-cat\';
elseif ( $category->term_id == $_current_category->parent )
$class .= \' current-cat-parent\';
}
$output .= \' class="\' . $class . \'"\';
$output .= ">$link\\n";
} else {
$output .= "\\t$link<br />\\n";
}
}
}
您可以进一步取出不需要的部件。
用法示例:
<?php
$args = array(
\'taxonomy\' => \'my_custom_taxonomy_slug\',
\'orderby\' => \'name\',
\'hide_empty\' => 0,
\'title_li\' => \'\',
\'hierarchical\' => 1,
\'walker\' => new Walker_Category_Find_Parents(),
);
?>
<ul class="menu">
<?php wp_list_categories( $args ); ?>
</ul>
输出示例:下面是一个列表示例,使用
Walker_Category_Find_Parents
步行者:
具有以下HTML结构:
<ul class="menu">
<li class="cat-item cat-item-1">
<a href="http://example.com/category/plants/">plants</a>
</li>
<li class="cat-item cat-item-2 i-have-kids">
<a href="http://example.com/category/animals/">animals</a>
<ul class="children">
<li class="cat-item cat-item-3 i-have-kids">
<a href="http://example.com/category/animals/birds/">birds</a>
<ul class="children">
<li class="cat-item cat-item-4">
<a href="http://example.com/category/animals/birds/falcons/">falcons</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="cat-item cat-item-5">
<a href="http://example.com/category/stones">stones</a>
</li>
</ul>
我刚刚删除了
title
属性以使其更具可读性。
但你可以看到i-have-kids
类添加到li
带有子项的标记。
当我访问/ Animals / Birds /
类别,HTML结构变为:
<ul class="menu">
<li class="cat-item cat-item-1">
<a href="http://example.com/category/plants/">plants</a>
</li>
<li class="cat-item cat-item-2 i-have-kids current-cat-parent">
<a href="http://example.com/category/animals/">animals</a>
<ul class="children">
<li class="cat-item cat-item-3 i-have-kids current-cat">
<a href="http://example.com/category/animals/birds/">birds</a>
<ul class="children">
<li class="cat-item cat-item-4">
<a href="http://example.com/category/animals/birds/falcons/">falcons</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="cat-item cat-item-5">
<a href="http://example.com/category/stones">stones</a>
</li>
</ul>