我需要显示我为自定义帖子类型创建的所有类别,然后在每个类别内,我需要循环与该类别相关的所有帖子。
我尝试了许多不同的方法来构建我的WP\\u查询,但我根本无法让它工作。
以下是我现在掌握的代码:
$categories = get_categories(\'taxonomy=faqcat&order=DESC\');
foreach ($categories as $cat) {
// loop through all posts tied to category here
}
更新的代码。。还是不行。。在每个类别中显示相同的帖子。
<?php
$categories = get_categories(\'taxonomy=faqcat&order=DESC\');
foreach ($categories as $cat) :
echo \'<h1>\' . $cat->name . \' (\' . $cat->cat_ID . \' )</h1>\';
$q = new WP_Query(array(\'cat_ID\' => $cat->cat_ID, \'post_type\' => \'faq\', \'tax_query\' => array(\'taxonomy\' => \'faqcat\')));
if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
echo $post->ID;
?>
<pre> <B><?php the_title(); ?></b></pre>
<p><?php the_content(); ?></p>
<br/>
<?php endwhile;
else:
?>
<p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>
<?php
endif;
endforeach;
?>
最合适的回答,由SO网友:user990717 整理而成
下面是我如何修复它的。。
<!--faq page -->
<div class="faq">
<?php get_template_part(\'./template/global/breadcrumbs\'); ?>
<h3><span><?php the_title(); ?></span></h3>
<?php
$categories = get_categories(\'taxonomy=faqcat&order=DESC\');
foreach ($categories as $cat) {
$i = 0;
?>
<div class="faq-title">
<h2><?php echo $cat->name; ?></h2>
</div>
<?php
$answers = new WP_Query(array(\'post_type\' => \'faq\', \'tax_query\' => array(array(\'taxonomy\' => \'faqcat\', \'field\' => \'id\', \'terms\' => $cat->term_id,),),));
if ($answers->have_posts()) : while ($answers->have_posts()) : $answers->the_post();
$i++;
?>
<div class="faq-post" id="faq-<?php echo $i; ?>"> <span class="close-tab"><?php the_title(); ?></span>
<div class="faq-post-detail">
<?php the_content(); ?>
</div>
</div>
<?php
endwhile;
else:
?>
<p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>
<?php endif;
}
?>
</div>
</div>
<!--/content -->