Отображать термины иерархического списка как флажки

Я пытаюсь преобразовать список иерархических терминов в флажки. Термины создаются с использованием приведенного ниже кода, но по умолчанию они отображаются в виде ссылок.

<?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>

Вот HTML, который <?php wp_list_categories( $args ); ?> выходы …

<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 являются родительскими терминами, и я не хочу, чтобы они были флажками, как мне все это сделать? 🙂

1

Решение

Попробуйте добавить этот класс в настройку ваших функций.

// Keep Categories ordered by group on backend
if ( ! class_exists( 'ftChangeTaxonomyCheckboxlistOrder' ) ){

class ftChangeTaxonomyCheckboxlistOrder {

function ftChangeTaxonomyCheckboxlistOrder(){

function changeTaxonomyCheckboxlistOrder( $args, $post_id)
{
if ( isset( $args['taxonomy']))
$args['checked_ontop'] = false;
return $args;
}

add_filter('wp_terms_checklist_args','changeTaxonomyCheckboxlistOrder',10,2);
}

} // class ends here

$fttaxonomychangeorder = new ftChangeTaxonomyCheckboxlistOrder();

}// top most if condition ends here
1

Другие решения

Как правило, данные будут храниться в древовидной модели, такой как Модель вложенного набора, который затем повторяется, иногда рекурсивно, для отображения результатов.

Полезный пример PHP File Tree, Кора LaViska, но, конечно, вам нужно изменить его для вывода желаемого HTML.

Если ваши данные имеют глубину только один уровень, вы можете поочередно создать трехмерный массив:

// Please note that this is just an example that you will have to adapt to your specific needs
$nodes = array();
// add each title:
$nodes['title1'] = array();

// add each node to the corresponding title:
// you will need to add 'id', 'rel', and probably 'label' for the checkbox output
$nodes['title1'][] = array(
'taxonomy'     => $taxonomy,
'orderby'      => $orderby,
'show_count'   => $show_count,
'pad_counts'   => $pad_counts,
'hierarchical' => $hierarchical,
'title_li'     => $title
);

// then iterate through them and display
foreach($nodes AS $title=>$node) {
echo "<h3>$title</h3>";
// and finally iterate through the child nodes to display your checkboxes:
foreach($node AS $child) {
// assuming you added the appropriate fields
echo '<label><input type="checkbox" id="' . $child['id'] . '" rel="' . $child['rel'] . '">' . $child['label'] . '</label>';
}
}
0