PHP 错误代码:试图获取非对象的属性

use*_*498 0 php wordpress

当我在我的 WordPress 网站上搜索时,我收到以下错误:注意:尝试在第 20 行的 archive-product.php 中获取非对象的属性

该 php 文件中的第 20 行是

$postid = $wp_query->post->ID;
Run Code Online (Sandbox Code Playgroud)

关于如何解决这个问题的任何建议?作为参考,第 20 行前后的立即代码如下。

<?php //Display Page Header
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo page_header( get_post_meta($postid, 'qns_page_header_image', true) );
    wp_reset_query();
?>
Run Code Online (Sandbox Code Playgroud)

谢谢你,瑞安

ben*_*las 5

所以这意味着$wp_query->post不是一个对象。我没有经常使用 worpress,但看起来其中一个帖子必须返回一个空对象。

试试这个以跳过任何空的帖子:

<?php //Display Page Header
    global $wp_query;
    if(!empty($wp_query->post)){
        $postid = $wp_query->post->ID;
        echo page_header( get_post_meta($postid, 'qns_page_header_image', true) );
    }
    wp_reset_query();
?>
Run Code Online (Sandbox Code Playgroud)