wp_list_categories 的自定义函数,用于将术语列为复选框
custom function for wp_list_categories to list terms as checkboxes
我需要为 wp_list_categories 编写一个自定义函数,因此它们不是显示分层链接列表,而是一个复选框列表,parent 术语显示为 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 标签中的标题是 parent 术语,我不想将它们作为复选框,我该怎么做? :)
也许你可以使用 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>';
}
}
您可以像这样使用 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/
我需要为 wp_list_categories 编写一个自定义函数,因此它们不是显示分层链接列表,而是一个复选框列表,parent 术语显示为 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 标签中的标题是 parent 术语,我不想将它们作为复选框,我该怎么做? :)
也许你可以使用 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>';
}
}
您可以像这样使用 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/