You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义taxonomy-category.php仅显示多文章分类内容,排查错误原因

问题排查与修复

你的代码存在两处关键问题,导致单篇文章无法正常展示:

  • 错误使用全局查询判断条件
    你调用了全局的have_posts()函数判断是否存在文章,但实际上你使用的是自定义查询对象$loop,必须通过$loop->have_posts()来检查该自定义查询的结果,而非全局主查询的状态。

  • wp_reset_postdata()位置错误
    你将wp_reset_postdata()放在了while循环内部,这会导致每次循环后立即重置帖子数据。单篇文章场景下,循环执行一次就重置数据,可能引发后续数据异常。正确的做法是将其移至endwhile之后,确保整个循环执行完毕再重置。

修复后的完整代码:

<?php 
$loop = mep_event_query(20, 'ASC', $term_id, '', '', '', 'upcoming'); 
if ($loop->have_posts() ): 
    while ( $loop->have_posts() ) : $loop->the_post();
        do_action('mep_event_list_shortcode', get_the_id(), 'four_column', 'grid'); 
    endwhile;
    wp_reset_postdata(); 
else: ?>
<p class="avis-no-cursos">En estos momentos no hay disponible ningun curso.</p>
<p class="boto-negre">
    <a href="https://www.url" class="custom-link btn btn-sm border-width-0 btn-default btn-icon-right btn-ripple-out btn-border-animated cursor-init">Ver cursos disponibles<i class="fa fa-angle-right"></i></a>
</p>
<?php endif;
?>

额外说明:原代码中while循环后的冗余大括号{}已移除,不影响功能但能让代码更简洁。

内容的提问来源于stack exchange,提问作者peresm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 00:05:13