[英]Wordpress - How do I display posts grouped by custom taxonomy?
I am creating a FAQ page using a custom post type and custom taxonomy.我正在使用自定义帖子类型和自定义分类法创建一个常见问题页面。 I'm trying to create an unordered list for each taxonomy in order to group the FAQ's.我正在尝试为每个分类法创建一个无序列表,以便对常见问题进行分组。 In that unordered list, I want the first listed item to be the taxonomy name and then repeat the second listed item for all the questions in the taxonomy.在该无序列表中,我希望列出的第一个项目是分类法名称,然后针对分类法中的所有问题重复列出的第二个项目。 This is the page I'm working on link .这是我正在处理的页面link 。
It's currently duplicating the posts instead of displaying in the rightful taxonomies.它目前正在复制帖子,而不是显示在合法的分类法中。
<?php
// get all the categories from the database
$cats = get_terms( array(
'taxonomy' => 'faq_categories',
));
// loop through the categories
foreach ($cats as $cat) {
// setup the category ID
$cat_id = $cat->term_id;
?>
<!-- Make a header for the category -->
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
// create a custom wordpress query
query_posts( array(
'post_type' => 'faqs',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories', //or tag or custom taxonomy
'field' => 'slug',
'terms' => 'for-women'
)
)
));
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category
wp_reset_postdata();
?>
</ul>
<?php } // done the foreach statement ?>
Your query is not changing as you iterate through the $cats
array.当您遍历$cats
数组时,您的查询不会改变。 Perhaps changing the the value of the 'terms' array to $cat->slug
would give you better results.也许将 'terms' 数组的值更改为$cat->slug
会给你更好的结果。
在您的query_post
,您的tax_query
的field
应该是term_id
并且您的terms
被分配给您的$cat_id
变量,而不是一个硬编码的条款。
Thank you so much.非常感谢。 You have both provided great insight as to what I was missing.你们都提供了关于我所缺少的东西的深刻见解。 I've resolved it now and this is how I went about solving it taking your suggestions into consideration.我现在已经解决了,这就是我在考虑您的建议的情况下解决它的方法。
<?php
$cats = get_terms(
array(
'taxonomy' => 'faq_categories',
'orderby' => 'term_id',
'order' => 'ASC'
)
);
foreach ($cats as $cat) :
?>
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
$questions = new WP_Query(
array(
'category_name' => $cat->slug
)
);
$questions = new WP_Query( array(
'post_type' => 'faqs',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories',
'field' => 'slug',
'terms' => array($cat->slug),
)
)
));
?>
<?php if ($questions->have_posts()) : while ($questions->have_posts()) : $questions->the_post();?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.