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

WordPress无标签文章预览时have_posts()调用报错排查

WordPress无标签文章预览报致命错误修复

问题现象

在WordPress主题的single.php模板中实现相关文章展示功能,用于在文章详情页输出与当前浏览文章匹配的推荐内容。测试时发现异常:

  • 新建文章未分配任何标签时,预览文章会抛出致命错误
  • 为文章添加标签后再预览,页面可正常渲染,无任何异常

抛出的错误信息:

Call to a member function have_posts () on null

问题原因

原代码逻辑存在分支遗漏:

  1. 代码先通过wp_get_post_tags()获取当前文章的标签,仅当标签存在时,才会初始化自定义查询对象$my_query = new WP_Query($args)
  2. 但后续输出相关文章列表的while ($my_query->have_posts())循环逻辑,写在了if ($tags)的判断范围之外
  3. 当文章没有标签时,$tags为空数组,判断不成立,$my_query根本没有被定义(值为null),此时直接调用null对象的have_posts()方法,就会触发致命错误。

修复方案

将所有依赖$my_query的相关文章输出代码,全部放到查询初始化、且查询有结果的判断分支内,同时在循环结束后重置文章数据,避免影响主循环。
修复后的核心代码结构如下:

<?php
// 获取当前文章标签
$tags = wp_get_post_tags($post->ID);
if ($tags) :
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    $args=array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'posts_per_page'=>6,
        'ignore_sticky_posts'=>1 // 原caller_get_posts参数已废弃,替换为等效官方参数
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) :
?>
<?php get_sidebar(); ?>
<div class="col-12 col-lg-7 offset-lg-1">
    <div id="powiazane">
        <h2> Powiązane artykuły: </h2>
        <div class="card-deck mb-50px">
        <?php
        $post_wrap = 0; 
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class('card border-0 ' . $termsString); ?>>
            <img class="card-img-top secondary-post-img p-3" src="<?php echo the_post_thumbnail_url('post-thumb'); ?>" alt="<?php the_title_attribute(); ?>">
            <div class="card-body">
                <a href="<?php the_permalink(); ?>" class="stretched-link" alt="<?php the_title_attribute(); ?>">
                    <?php
                        if(get_field('tytul_krotki'))
                        {
                        echo '<h3>' . get_field('tytul_krotki') . '</h3>';
                        } else {
                        echo '<h3>' . get_the_title() . '</h3>';
                        }
                    ?>
                </a>
                <div class="small text-muted">
                    <p class="mb-0">
                        <?php the_field('miasto'); ?> | <?php echo hubdab_relative_time(); ?>
                    </p>
                </div>
                <p class="mt-25px"><?php the_field('lid_krotki') ?> </p>
            </div>
            <div class="card-footer py-0">
            </div>
        </article><!-- #post-## -->
        <?php
        if ($post_wrap % 2 == 1){
            echo '<div class="w-100 d-none d-sm-block"><!-- wrap every 2 on sm+--></div>';
        }   
        $post_wrap++; ?> 
        <?php
        endwhile;
        // 重置全局文章对象,避免自定义查询污染主循环
        wp_reset_postdata();
        ?>
        </div>
    </div>
</div>
<?php
    endif; // 结束$my_query查询结果判断
endif; // 结束标签存在判断
?>

内容的提问来源于stack exchange,提问作者Jakub Lipiński

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 21:00:56