php - 嵌套的 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>
所以总结了我的问题:
- 在 page-product.php 页面上创建过滤器
- 正确嵌套列表
谢谢!
解决方案
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>
推荐阅读
- ios - 检测 iphone 是刘海屏还是普通矩形屏以避免导航栏大小问题?
- java - 贪心算法 - 没主意
- ruby-on-rails - 在 Rails 模型范围内强制执行特定顺序
- javascript - 在 Angular 中动态传递接口作为参数
- python - 直接从 SFTP 服务器将音频文件加载到 Python 的语音识别模块(使用 Paramiko SFTPClient)
- css - 不会在 IE 上执行的 CSS
- ios - Swift - 存储视图的堆叠顺序
- javascript - target.status 值未定义
- node.js - 尝试使用 sinon 监视函数但出现错误
- visual-studio-2010 - 配置 Bamboo 以使用 MS Build Xaml 模板