显示“通知:未定义的变量:”和“通知:正在尝试获取非对象的属性”

时间:2014-09-01 作者:Alamin

使用以下代码时显示“未定义变量”和“尝试获取非对象的属性”通知

  while($q->have_posts()) : $q->the_post();

$newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), \'\', false, \'\' );

$list .= \'<li class="news-item">
<table cellpadding="4">
    <tr>
        <td>
             <img src="\'.$newsbox_post_img_src[0].\'" width="100" class="img-circle" />
       </td>
       <td>\'.get_the_excerpt().\'</td>
    </tr>
</table>
    </li>\';

endwhile;
以下是错误:

enter image description here

根据评论更新:

$q 来自

function newsbox_new_loading_shortcod($atts){
      extract(shortcode_atts(array(
        \'posts_per_page\'=>\'-1\',
        \'news_box_title\'=>\'Latest News\',
        \'post_type\'=>\'post\',
        \'category\'=>\'\'  ),$atts,\'news_box\'));
        $q = new WP_Query(
              array(\'posts_per_page\' => $posts_per_page,\'post_type\'=>array($post_type),\'category_name\'=>$category));

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

EDIT

我已经重写了你的全部短代码。我已经测试了我原始答案中的代码,结合您的代码,输出有点出乎意料。

重大修改

我删除了类别,并将其替换为tax_query (参见WP_Query). 原因是tax_query 可用于内置类别和自定义分类法,自定义分类法通常与自定义帖子类型一起使用

重新调整了查询的结构,使其更具可操作性和可读性。调试也更好

请注意:此短代码至少需要PHP 5.4

add_shortcode( \'news_box\', \'newsbox_new_loading_shortcode\' );
function newsbox_new_loading_shortcode($atts){
    ob_start();
    $a = shortcode_atts( 
        [
            \'posts_per_page\'    => \'-1\',
            \'news_box_title\'    => \'Latest News\',
            \'post_type\'         => \'post\',
            \'taxonomy\'          => \'\',
            \'terms\'             => \'\',
        ], 
        $atts 
    );


    if( \'\' == $a[\'taxonomy\'] || \'\' == $a[\'terms\'] ) {
        $args = [
            \'posts_per_page\'    => $a[\'posts_per_page\'],
            \'post_type\'         => $a[\'post_type\'],
        ];
    }else{
        $args = [
            \'posts_per_page\'    => $a[\'posts_per_page\'],
            \'post_type\'         => $a[\'post_type\'],
            \'tax_query\'         => [
                [
                    \'taxonomy\'  => $a[\'taxonomy\'],
                    \'field\'     => \'slug\',
                    \'terms\'     => $a[\'terms\'],
                ]
            ]
        ];
    }
    $q = new WP_Query($args);

    if ( $q->have_posts() ) : 
        while($q->have_posts()) : $q->the_post();   
            $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), \'\', false, \'\' ); ?>

            <li class="news-item">
                <table cellpadding="4">
                    <tr>
                        <td>
                            <?php if( !empty($newsbox_post_img_src)) { ?>
                                <img src="<?php echo $newsbox_post_img_src[0]; ?>" width="100" class="img-circle" />
                            <?php } ?>      
                        </td>
                        <td>
                            <?php the_excerpt(); ?>
                        </td>
                    </tr>
                </table>
            </li>

        <?php endwhile;
        $list = ob_get_clean();

        return $list;
    endif;

    wp_reset_postdata();
}
如果需要使其与5.4以下的PHP版本兼容,只需将其更改为

add_shortcode( \'news_box\', \'newsbox_new_loading_shortcode\' );
function newsbox_new_loading_shortcode($atts){
    ob_start();
    $a = shortcode_atts( 
        array(
            \'posts_per_page\'    => \'-1\',
            \'news_box_title\'    => \'Latest News\',
            \'post_type\'         => \'post\',
            \'taxonomy\'          => \'\',
            \'terms\'             => \'\',
        ), 
        $atts 
    );


    if( \'\' == $a[\'taxonomy\'] || \'\' == $a[\'terms\'] ) {
        $args = array(
            \'posts_per_page\'    => $a[\'posts_per_page\'],
            \'post_type\'         => $a[\'post_type\'],
        );
    }else{
        $args = array(
            \'posts_per_page\'    => $a[\'posts_per_page\'],
            \'post_type\'         => $a[\'post_type\'],
            \'tax_query\'         => array(
                array(
                    \'taxonomy\'  => $a[\'taxonomy\'],
                    \'field\'     => \'slug\',
                    \'terms\'     => $a[\'terms\'],
                ),
            ),
        );
    }

    update_option(\'_post_type_name\', $a[\'post_type\']);

    $q = new WP_Query($args);

    if ( $q->have_posts() ) : 
        while($q->have_posts()) : $q->the_post();   
            $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), \'\', false, \'\' ); ?>

            <li class="news-item">
                <table cellpadding="4">
                    <tr>
                        <td>
                            <?php if( !empty($newsbox_post_img_src)) { ?>
                                <img src="<?php echo $newsbox_post_img_src[0]; ?>" width="100" class="img-circle" />
                            <?php } ?>      
                        </td>
                        <td>
                            <?php the_excerpt(); ?>
                        </td>
                    </tr>
                </table>
            </li>

        <?php endwhile;
        $list = ob_get_clean();

        return $list;
    endif;

    wp_reset_postdata();
}

function newsbox_excerpt_more($more) {
    global $post;
    $post_type_name = get_option(\'_post_type_name\');
    if( \'post\' != $post_type_name ){
        $read_more_text = "TEXT FOR ALL THAT DOES NOT HAVE post POST TYPE";
    }
    else {
        $read_more_text = "Read More &raquo;";
    }
    return \'...<a href="\'. get_permalink($post->ID) . \'">\' . $read_more_text . \'</a>\';
}
add_filter(\'excerpt_more\', \'newsbox_excerpt_more\');
然后,您可以使用您的短代码,如下所示

[news_box posts_per_page=-1 news_box_title="Latest News" post_type="event_type"  taxonomy="event_cat"  terms="testcat"]

[news_box posts_per_page=-1 news_box_title="Latest News" post_type="post"  taxonomy="category"  terms="testcategory"]

[news_box posts_per_page=-1 news_box_title="Latest News" post_type="post"]

ORIGINAL ANSWER

您不需要使用$post->ID 使用时get_post_thumbnail_id 在回路内部。默认情况下,将使用当前帖子ID,因此无需指定ID。此外,您还需要实际检查$newsbox_post_img_src 有东西要返回,否则会返回

尝试获取非对象的属性

错误另一件需要注意的事情是,您应该使用wp_reset_postdata 和来自短代码的输出应为returned,而不是echo\'D

您的代码基本上应该是这样的

while($q->have_posts()) : $q->the_post();   
    $newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), \'\', false, \'\' );

    $list = \'<li class="news-item">\';
    $list .= \'<table cellpadding="4">\';
    $list .= \'<tr>\';
    $list .= \'<td>\';
    if( !empty($newsbox_post_img_src)) {
        $list .= \'<img src="\'.$newsbox_post_img_src[0].\'" width="100" class="img-circle" />\';
    }       
    $list .= \'</td>\';
    $list .= \'<td>\'.get_the_excerpt().\'</td>\';
    $list .= \'</tr>\';
    $list .= \'</table>\';
    $list .= \'</li>\';
    return $list;

endwhile;
wp_reset_postdata();

SO网友:Elisha Terada

我看到您的代码中有几个问题可能会导致您的总体问题。试着解决下面的问题,看看你能否继续前进。

语法错误:

// Syntax here would cause error
$q = new WP_Query(
array(\'posts_per_page\' => \'posts_per_page,\'post_type\'=>array($post_type),\'category_name\'=>$category));
解决类似的问题

$q = new WP_Query(
array(\'posts_per_page\' => $posts_per_page, \'post_type\'=>array($post_type), \'category_name\' => $category));
不必要的第四个参数:

// Documentation for wp_get_attachment_image_src
// http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

// You don\'t need forth argument on this line
$newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), \'\', false, \'\' );
解决类似的问题

$newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), \'\', false );
我的建议是确保代码的每一行在语法和用法方面都是有效的。否则,您将在调试过程中迷失方向。

结束

相关推荐

Displaying oEmbed errors?

有时,通过oEmbed嵌入项目是不可能的,例如,当YouTube视频已禁用嵌入时。The oEmbed service will return a 401 Unauthorized, 并且不会转换代码。有没有办法通知用户这一点?当前的工作流是非直观的(至少对我来说),我更喜欢在WordPress页面上,或者更好的是,在编辑器中显示一条消息,说明对象无法嵌入。