我需要为wp_list_categories编写自定义函数,因此不是显示链接的分层列表,而是显示一个复选框列表,其中父条款显示为h3标记。
最终输出看起来像http://jsfiddle.net/amesy/kwqpf5fv/6/
这是我的WordPress模板文件中的PHP代码......
<?php
//list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)
$taxonomy = 'tags';
$orderby = 'name';
$show_count = 1; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title
);
?>
<ul class="categories">
<?php wp_list_categories( $args ); ?>
</ul>
以下是<?php wp_list_categories( $args ); ?>
输出的HTML
<ul class="categories">
<li class="cat-item cat-item-21"><a href="http://tandsdev.co.uk/portfoliotags/client/" >Client</a> (0)
<ul class='children'>
<li class="cat-item cat-item-22"><a href="http://tandsdev.co.uk/portfoliotags/bmw/" >BMW</a> (3)
</li>
</ul>
</li>
<li class="cat-item cat-item-25"><a href="http://tandsdev.co.uk/portfoliotags/section/" >Section</a> (0)
<ul class='children'>
<li class="cat-item cat-item-27"><a href="http://tandsdev.co.uk/portfoliotags/automotive/" >Automotive</a> (3)
</li>
<li class="cat-item cat-item-28"><a href="http://tandsdev.co.uk/portfoliotags/property/" >Property</a> (2)
</li>
</ul>
</li>
<li class="cat-item cat-item-26"><a href="http://tandsdev.co.uk/portfoliotags/service/" >Service</a> (0)
<ul class='children'>
<li class="cat-item cat-item-29"><a href="http://tandsdev.co.uk/portfoliotags/branding/" >Branding</a> (3)
</li>
<li class="cat-item cat-item-30"><a href="http://tandsdev.co.uk/portfoliotags/email/" >Email</a> (3)
</li>
<li class="cat-item cat-item-31"><a href="http://tandsdev.co.uk/portfoliotags/website/" >Website</a> (2)
</li>
</ul>
</li>
</ul>
我希望每个术语显示的复选框代码将形成一个过滤系统,可在此处查看http://jsfiddle.net/amesy/kwqpf5fv/6/
<div class="tags">
<h3>service</h3>
<label><input type="checkbox" id="type-Website" rel="Website">Website</label>
<label><input type="checkbox" id="type-Email" rel="Email">Email</label>
<label><input type="checkbox" id="type-Branding" rel="Branding">Branding</label>
<h3>sector</h3>
<label><input type="checkbox" id="type-Automotive" rel="Automotive">Automotive</label>
<label><input type="checkbox" id="type-Property" rel="Property">Property</label>
</div>
我仍然希望将复选框保持分层,如上例所示,但h3标签中的标题是父条款,我不希望这些作为复选框,我该如何做到这一切? :)
答案 0 :(得分:8)
也许您可以使用get_categories()
代替wp_list_categories()
。
首先,您使用parent => 0
获取顶级类别,并在h3标记中显示类别名称。然后,您可以使用parent => $category->term_id
获取子类别,并显示复选框。
使用类似下面示例的内容:
$args = array(
'taxonomy' => 'tags',
'parent' => 0, // get top level categories
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'pad_counts' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ){
echo '<h3>'. $category->name . '</h3>';
$sub_args = array(
'taxonomy' => 'tags',
'parent' => $category->term_id, // get child categories
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'pad_counts' => 0
);
$sub_categories = get_categories( $sub_args );
foreach ( $sub_categories as $sub_category ){
echo '<label><input type="checkbox" id="type-'. $sub_category->name . '" rel="'. $sub_category->name . '">'. $sub_category->name . '</label>';
}
}
答案 1 :(得分:0)
您可以像这样使用wp_category_checklist()
:
<form>
<?php
$args = array(
'descendants_and_self' => 0,
'selected_cats' => false,
'popular_cats' => false,
'walker' => null,
'taxonomy' => 'tags',
'checked_ontop' => true
);
wp_terms_checklist( 0, $args );
?>
<input type="submit" value="Submit">
</form>
获取更多帮助: https://developer.wordpress.org/reference/functions/wp_terms_checklist/