我试图显示一个油炸圈饼图的分类名称及其计数。这就是我所做的;
<ul class="pie-chart__legend" id="donut1">
<?php
$wcatTerms1 = get_terms(\'incident_type\', array(\'hide_empty\' => 0, \'parent\' =>0));
foreach($wcatTerms1 as $wcatTerm1)
{
$args1 = array(
\'posts_per_page\' => -1,
\'post_type\' => \'incident\',
\'post_author\' => $current_user->ID,
\'post_status\' => \'publish\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'incident_type\',
\'field\' => \'term_id\',
\'terms\' => $wcatTerm1,
\'include_children\' => false
)
));
$count1 = 0;
$query1 = new WP_Query($args1);
while ( $query1->have_posts() )
{
$count1++;
}
?>
<li><em><?php echo $wcatTerm1->name; ?></em><span><?php echo $count1; ?></span></li>
<?php
}
?>
但这是无限的,最终会超时。我尝试了几个涉及\\u posts()的修复方法,但没有成功。
请让我知道我做错了什么。
最合适的回答,由SO网友:Thowzif 整理而成
我已经弄明白了。我失踪了$query1->the_post();
在while循环和wp_reset_postdata();
循环结束后。
最终工作代码:
<ul class="pie-chart__legend" id="donut1">
<?php
$wcatTerms1 = get_terms(\'incident_type\', array(\'hide_empty\' => 0, \'parent\' =>0));
foreach($wcatTerms1 as $wcatTerm1)
{
$args1 = array(
\'posts_per_page\' => -1,
\'post_type\' => \'incident\',
\'post_author\' => $current_user->ID,
\'post_status\' => \'publish\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'incident_type\',
\'field\' => \'term_id\',
\'terms\' => $wcatTerm1,
\'include_children\' => false
)
));
$count1 = 0;
$query1 = new WP_Query($args1);
while ( $query1->have_posts() )
{
$query1->the_post();
$count1++;
}
?>
<li><em><?php echo $wcatTerm1->name; ?></em><span><?php echo $count1; ?></span></li>
<?php
wp_reset_postdata();
}
?>
</ul>