在null上调用成员函数have_posts()

时间:2018-05-31 06:58:49

标签: php

我正在构建一个相关的帖子php脚本,我收到一个错误。 这段代码的全部内容在单个帖子中运行良好,但在导航到页面时会显示此错误。

Fatal error: Uncaught Error: Call to a member function have_posts() on null in /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-content/themes/genesis-sample/functions.php:207
Stack trace:
#0 /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-includes/class-wp-hook.php(286): mt_related_posts('')
#1 /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
#2 /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#3 /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-content/themes/genesis/lib/structure/loops.php(130): do_action('genesis_after_e...')
#4 /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-content/themes/genesis/lib/structure/loops.php(41): genesis_standard_loop()
#5 /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-includes/class-wp-hook.php(286): genesis_do_loop('')
#6 /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-includes/class-wp-hook.php(310): WP_Hook->apply_f
in /srv/bindings/62ea52c1ddf441b78293ae6364fa94dc/code/wp-content/themes/genesis-sample/functions.php on line 207

因为它显示错误在207行并且该行是

$the_related_posts = new WP_Query($args);}

    if( $the_related_posts->have_posts() ) :
        while ($the_related_posts->have_posts()) : $the_related_posts->the_post(); ?>

相关帖子脚本的完整代码是。那我现在能做什么请详细告诉我。记得iam为wordpress中的创世主题构建相关的帖子脚本

add_action( 'genesis_after_entry_content', 'mt_related_posts', 12 );

function mt_related_posts() { 
$cats = get_the_category();
if ( $cats ) { ?>

    <div class="mt-related-posts">

    <div class="wt-container">
        <h4 class="widget-title"><?php _e( 'Related Posts', 'magtheme' ); ?></h4>
    </div>

    <div class="mtrp-container">

        <?php

        $first_category = esc_attr( $cats[0]->term_id );
        $args = array(
            'cat'                   => array($first_category),
            'post__not_in'          => array($post->ID),
            'posts_per_page'        => 3,
            'ignore_sticky_posts'   => true
        );

        $the_related_posts = new WP_Query($args);}

        if( $the_related_posts->have_posts() ) :
            while ($the_related_posts->have_posts()) : $the_related_posts->the_post(); ?>

                <div class="mt-rel-post">
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute();?>">
                        <?php the_post_thumbnail( 'magtheme-related' ); ?>
                    </a>
                    <h3 class="post-title">
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                            <?php echo get_the_title( $ID );  ?>
                        </a>
                    </h3>
                    <p class="mt-meta"><?php echo the_date(); ?></p>
                </div>

            <?php
            endwhile;
        endif;

        wp_reset_postdata();

        ?>

    </div>
    </div>

    <?php

}

1 个答案:

答案 0 :(得分:0)

那是因为您的$the_related_posts变量为空。 在执行此类条件时,请检查var是否包含某些内容。

if($the_related_posts && $the_related_posts->have_posts()) {...}