将分层列表术语显示为复选框

时间:2015-01-05 17:21:58

标签: php wordpress filtering custom-taxonomy wp-list-categories

我正在尝试将层级术语列表转换为复选框。这些术语是使用下面的代码生成的,但默认情况下它们显示为链接。

<?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标签中的标题是父条款,我不希望这些作为复选框,我该如何做到这一切? :)

2 个答案:

答案 0 :(得分: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 :(得分:0)

通常,数据将存储在树模型中,例如Nested Set Model,然后迭代,有时递归,以显示结果。

一个有用的例子是Cora LaViska的PHP File Tree,但当然你需要修改它以输出所需的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>';
  }
}