Php 嵌套wordpress术语列表-筛选器

Php 嵌套wordpress术语列表-筛选器,php,jquery,wordpress,custom-post-type,taxonomy-terms,Php,Jquery,Wordpress,Custom Post Type,Taxonomy Terms,我用插件CPT UI在Wordpress中创建了一个自定义术语分类法。因此,我可以将“产品类别”添加到我的自定义帖子类型“产品”。 我们有父类别和子类别。例如: *脚趾控制 电动斯隆 软件 旺德莱泽 *明镜 维利希特明镜 等等 因此,我创建了一个新页面“页面产品”,以显示所有产品。此页面必须包含基于产品类别的筛选器 我猜这个页面由两个“循环”组成。一个用于过滤器,一个用于产品 产品循环 <?php $args = array('post_type' => 'product

我用插件CPT UI在Wordpress中创建了一个自定义术语分类法。因此,我可以将“产品类别”添加到我的自定义帖子类型“产品”。 我们有父类别和子类别。例如:

*脚趾控制

  • 电动斯隆
  • 软件
  • 旺德莱泽

*明镜

  • 维利希特明镜
等等

因此,我创建了一个新页面“页面产品”,以显示所有产品。此页面必须包含基于产品类别的筛选器

我猜这个页面由两个“循环”组成。一个用于过滤器,一个用于产品

产品循环

<?php $args = array('post_type' => 'product'); ?>
<?php $loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <?php get_template_part( 'loop-templates/content-product' ); ?>

<?php endwhile; ?>
<?php else: ?>
                <h1>
                    <?php
                        _e('Geen producten gevonden','axces-theme');
                    ?>
                </h1>
 <?php endif; ?>
 <?php wp_reset_postdata(); ?>


术语//过滤器*

                <?php
                    $args = array('hide_empty' => false, 'orderby' => 'term_group', 'parent' => false);
                    $terms = get_terms('product_categorie', $args);
                    $hierarchy = _get_term_hierarchy('product_categorie');
                    echo '<ul class="filter">';
                    foreach ($terms as $term) {
                      echo '<li class="parent"><strong class="parent__item">'.$term->name.'</strong>';
                      if (array_key_exists($term->term_id, $hierarchy)) {
                        echo '<ul class="childs">';
                        foreach ($hierarchy[$term->term_id] as $v) {
                          $child = get_term($v);
                          echo '<li class="child" data-filter="'.$child->slug.'">'.$child->name.'</li>';
                        }
                        echo '</ul>';
                      }
                      echo '</li>';
                    }
                    echo '</ul>';
                ?>

但这样做的结果并不正确。我想要一个嵌套列表。像这样:

<ul>
    <li class="parent">Toegangscontroles
        <ul>
            <li class="child">Software</li>
            <li class="child">Wandlezer</li>
        </ul>
    </li>
    <li class="parent">Another parent term
        <ul>
            <li class="child">child term</li>
            <li class="child">child term</li>
        </ul>
    </li>
</ul>
    • 软件
    • Wandlezer
  • 另一个父术语
    • 子术语
    • 子术语
因此,我总结了我的问题:

  • 在page-product.php页面上创建一个过滤器
  • 正确嵌套列表

  • 谢谢

    这将输出为您的产品注册的分类:

    之后,您需要一个foreach或一些迭代器来获取每个分类法的术语,如:

    <?php 
    
    $taxonomies = get_object_taxonomies( 'Product', 'objects');
    
    foreach($taxonomies as $taxonomy): ?>
      <h2> <?= $taxonomy->label ?> </h2>
    
      // Get all the single taxonomy terms
      $terms = get_terms( array(
        'taxonomy' => $taxonomy->name,
        'hide_empty' => false,
      ) );
    
      <ul>
        <?php foreach($terms as $term): ?>
          <li><?= $term->name ?></li>
         <?php endforeach; ?>
      </ul>
    
    
    <?php endforeach; ?>
    
    
    //获取所有单一的分类术语
    $terms=get_terms(数组(
    'taxonomy'=>$taxonomy->name,
    “hide_empty”=>false,
    ) );
    

    HTML在这里无效,但是正确的解决方案是使用_get\u term\u hierarchy()函数

    $args=array('hide_empty'=>0,'orderby'=>'term_group');
    $terms=get_terms('category',$args);
    $hierarchy=_get_term_hierarchy('category');
    回声“
      ”; foreach($terms作为$term){ 回显“
    • ”.$term->name; 如果(数组\u键\u存在($term->term\u id,$hierarchy)){ 回声“
        ”; foreach($v的层次结构[$term->term\u id]{ $child=get_项($v); 回显“
      • ”.$child->name.“
      • ”; } 回声“
      ”; } 回音“
    • ”; } 回声“
    ”;
    使用
    wp\u术语检查表
    'taxonomy'=>'taxonomy'
    参数

    $args = array(
        'descendants_and_self'  => 0,
        'selected_cats'         => false,
        'popular_cats'          => false,
        'walker'                => null,
        'taxonomy'              => 'your taxomonomy',
        'checked_ontop'         => true
    );
     
    <ul>
        <?php wp_terms_checklist(0, $args); ?>
    </ul>
    
    $args=array(
    “后代和自身”=>0,
    “选定的_猫”=>错误,
    “受欢迎的猫”=>错误,
    'walker'=>空,
    “分类法”=>“你的分类法”,
    “已选中”选项=>为真
    );
    

    首先,您必须获取所有父类别,然后根据父id,您可以编写一个循环函数,为您提供给定id的子类别。请检查下面的代码

    function get_child_categories( $parent_category_id ){
        $html = '';
        $child_categories = get_categories( array( 'parent' => $parent_category_id, 'hide_empty' => false, 'taxonomy' => 'product_cat' ) );
        if( !empty( $child_categories ) ){
            $html .= '<ul>';
            foreach ( $child_categories as $child_category ) {
                $html .= '<li class="child">'.$child_category->name;
                $html .= get_child_categories( $child_category->term_id );
                $html .= '</li>';
            }
            $html .= '</ul>';
        }
        return $html;
    }
    
    function list_categories(){
        $html = '';
        $parent_categories = get_categories( array( 'parent' => 0, 'hide_empty' => false, 'taxonomy' => 'product_cat' ) );
        $html.= '<ul>';
        foreach ( $parent_categories as $parent_category ) {
            $html .= '<li class="parent">'.$parent_category->name;
            $html .= get_child_categories( $parent_category->term_id  );
            $html .= '</li>';
        }
        $html.= '</ul>';
        return $html;
    }
    add_shortcode( 'list_categories', 'list_categories' );
    
    函数获取子类($parent\u category\u id){
    $html='';
    $child\u categories=get\u categories(数组('parent'=>$parent\u category\u id,'hide\u empty'=>false,'taxonomy'=>product\u cat');
    如果(!空($child_categories)){
    $html.='
      '; foreach($child\u categories作为$child\u categories){ $html.='
    • 。$child\u类别->名称; $html.=获取子类别($child\u category->term\u id); $html.='
    • '; } $html.='
    '; } 返回$html; } 功能列表_类别(){ $html=''; $parent_categories=get_categories(数组('parent'=>0,'hide_empty'=>false,'taxonomy'=>product_cat'); $html.='
      '; foreach($parent\u categories作为$parent\u categories){ $html.='
    • 。$parent\u category->name; $html.=获取子类别($parent\u category->term\u id); $html.='
    • '; } $html.='
    '; 返回$html; } 添加_短代码('list_categories'、'list_categories');
    输出

    • A-主要类别
      • B-儿童类别
      • C-儿童类别
        • D-嵌套子类别(如果可用)
    • E-主要类别
      • F-子类别
      • G-子类别
        • H-嵌套子类别(如果可用)

    这只是显示一个ul,其中包含
  • 项中的所有术语。孩子没有嵌套在父对象中。几乎…:-)这将显示父类别和嵌套的子类别。但我显示的子类别与父类别相同。之后,它转到下一个父类别“但是我显示了与父类别相同的子类别”-抱歉,我没有注意到这一点,但是猜测您可以尝试将$args编辑为$args=array('hide_empty'=>0,'orderby'=>'term_group','parent'=>0);我已经用工作代码编辑了我的术语循环。@emeson在这种情况下,您可以尝试
    wp\u terms\u检查表
    YES!这正是我一直在寻找的。如果你更新你的答案,我会给你奖金。
    function get_child_categories( $parent_category_id ){
        $html = '';
        $child_categories = get_categories( array( 'parent' => $parent_category_id, 'hide_empty' => false, 'taxonomy' => 'product_cat' ) );
        if( !empty( $child_categories ) ){
            $html .= '<ul>';
            foreach ( $child_categories as $child_category ) {
                $html .= '<li class="child">'.$child_category->name;
                $html .= get_child_categories( $child_category->term_id );
                $html .= '</li>';
            }
            $html .= '</ul>';
        }
        return $html;
    }
    
    function list_categories(){
        $html = '';
        $parent_categories = get_categories( array( 'parent' => 0, 'hide_empty' => false, 'taxonomy' => 'product_cat' ) );
        $html.= '<ul>';
        foreach ( $parent_categories as $parent_category ) {
            $html .= '<li class="parent">'.$parent_category->name;
            $html .= get_child_categories( $parent_category->term_id  );
            $html .= '</li>';
        }
        $html.= '</ul>';
        return $html;
    }
    add_shortcode( 'list_categories', 'list_categories' );