gpt4 book ai didi

php - wp_list_categories 的自定义函数将术语列为复选框

转载 作者:行者123 更新时间:2023-12-04 23:11:40 31 4
gpt4 key购买 nike

我需要为 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 标签中的标题是父术语,我不想将它们作为复选框,我该怎么做呢? :)

最佳答案

也许您可以使用 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>';

}

}

关于php - wp_list_categories 的自定义函数将术语列为复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27795447/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com