首页 > 解决方案 > 嵌套的 wordpress 术语列表 - 过滤器

问题描述

我使用插件 CPT UI 在 Wordpress 中制作了自定义术语分类法。所以我可以将“产品类别”添加到我的自定义帖子类型“产品”中。我们有父类和子类。例如:

*Toegangs控制


*明镜

等等

所以我创建了一个新页面“page-products”来显示所有产品。此页面必须包含基于产品类别的过滤器。

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

产品循环

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

所以总结了我的问题:

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

谢谢!

标签: phpjquerywordpresscustom-post-typetaxonomy-terms

解决方案


wp_terms_checklist'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>

推荐阅读