显示不同结构的WordPress类别

时间:2017-01-14 作者:sajadk

我想用不同的结构显示所有wordpress类别,我不想使用列表结构。我想在结构中选择自己的元素,如下所示:

<div> catname <input type="hidden"> <div class="sub">subname<input><div class="secondsub"></div></div></div>
请引导我。谢谢

1 个回复
最合适的回答,由SO网友:funkysoul 整理而成

这里有一个链接指向我的要点,您可以根据需要简单地将ul li替换为DIV。

调用函数getCatTree将导致在ul/li构造中输出所有类别以及相应的子级。现在,您只需要定制css/js,使其与您想要的外观和功能相匹配。

Replace product_cat with the taxonomy you need

//GET CATEGORIES FOR NAVIGATION
add_action(\'tauchbar_get_cats\', \'getCatTree\', 15);
function getCatTree(){
  $taxonomy     = \'product_cat\';
  $orderby      = \'name\';  
  $show_count   = 0;      // 1 for yes, 0 for no
  $pad_counts   = 0;      // 1 for yes, 0 for no
  $hierarchical = 1;      // 1 for yes, 0 for no  
  $title        = \'\';  
  $empty        = 0;

  $args = array(
         \'taxonomy\'     => $taxonomy,
         \'orderby\'      => $orderby,
         \'show_count\'   => $show_count,
         \'pad_counts\'   => $pad_counts,
         \'hierarchical\' => $hierarchical,
         \'title_li\'     => $title,
         \'hide_empty\'   => $empty,
         \'exclude\' => array( 104 )
  );

  echo \'<ul class="nav_categories">\';
  $all_categories = get_categories( $args );
  foreach ($all_categories as $cat) {
    if($cat->category_parent == 0) {
        $category_id = $cat->term_id;       

        echo \'<li data-catname="\'.$cat->name.\'" class="cat_button"><a href="\'. get_term_link($cat->slug, \'product_cat\') .\'">\'. $cat->name .\'</a>\';

        $args2 = array(
                \'taxonomy\'     => $taxonomy,
                \'child_of\'     => 0,
                \'parent\'       => $category_id,
                \'orderby\'      => $orderby,
                \'show_count\'   => $show_count,
                \'pad_counts\'   => $pad_counts,
                \'hierarchical\' => $hierarchical,
                \'title_li\'     => $title,
                \'hide_empty\'   => $empty
        );

        $sub_cats = get_categories( $args2 );

        if($sub_cats) {
          echo \'<ul class="sub_cat">\';
            foreach($sub_cats as $sub_category) {

                echo \'<a href="\' . get_term_link($sub_category->slug, \'product_cat\') .\'">\';
                /*$thumbnail_id = get_woocommerce_term_meta( $sub_category->term_id, \'thumbnail_id\', true );
                $image = wp_get_attachment_url( $thumbnail_id );
                echo \'\';*/
                echo "<p>" . $sub_category->name . "</p></a>";
            }   
            echo \'</ul></li>\';
        } else {
          echo \'</li>\';
        }
    }       
  }
  echo \'</ul>\';
}

https://gist.github.com/funkysoul/c27bb013adb9cb847cfff4f5ee8c3847