嵌套的WordPress术语列表-过滤器

时间:2018-10-24 10:50:50

标签: php jquery wordpress custom-post-type taxonomy-terms

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

* Toegangscontrols

  • 电子烟气
  • 软件
  • 魔杖

* Spiegels

  • Verlichte spiegels
  • ...

以此类推

因此,我创建了一个新页面“ 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. 正确嵌套列表

谢谢!

4 个答案:

答案 0 :(得分:2)

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>

答案 1 :(得分:1)

HTML在这里无效,但是正确的解决方案是使用_get_term_hierarchy()函数

JSON.stringify()

答案 2 :(得分:0)

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

之后,您希望让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; ?>

答案 3 :(得分:0)

首先,您必须获取所有父类别,然后根据父 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' );

输出

  • A - 主要类别
    • B - 子类别
    • C - 子类别
      • D - 嵌套子类别(如果有)
  • E - 主要类别
    • F - 儿童类别
    • G - 子类别
      • H - 嵌套子类别(如果有)
相关问题