0

Showing "Undefined variable" and "Trying to get property of non-object" notice when using the following code

  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;

Here are the errors:

enter image description here


Update from comment:

$q comes from

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));
  • Where is the query for the variable '$q'? – WisdmLabs Sep 1 '14 at 19:19
  • 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)); – Alamin Sep 1 '14 at 19:43
  • 1
    There are any posts? Anyway, you could try with get_the_ID() – Tomás Cot Sep 1 '14 at 21:24
  • Use get_the_ID() or global $post – Laxmana Sep 1 '14 at 21:38
1

EDIT

I have rewritten your complete shortcode. I've tested the code from my original answer, and with your code combined, the output was a bit unexpected.

Major modifications

  • I've removed category and replaced it with a tax_query (see WP_Query). The reason is, a tax_query can be used for build-in categories and for custom taxonomies, and custom taxonomies are usually used with custom post types

  • Rearranged the structure of the query a bit to make it more workable and more readable. It is also better to debug

Please Note: This shortcode needs at least 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();
}

If you need to make it compatible with PHP versions below 5.4, just change it to

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');

You can then use your shortcode as follows

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

or

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

or

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

ORIGINAL ANSWER

You don't need to use $post->ID when using get_post_thumbnail_id inside the loop. By default, the current post ID is used, thus unnecessary to specify an ID. Also, you will need to actually check if $newsbox_post_img_src has something to return, otherwise it will return a

Trying to get property of non-object

error. Another thing to note, you should reset your custom loop with wp_reset_postdata and output from a shortcode should be returned, and not echo'd

Your code should basically look something like this

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();
  • Please see my edit. Hope this is helpful – Pieter Goosen Sep 2 '14 at 16:55
1

There are couple issues I see with your code that are probably contributing to your overall problem. Try fix the issues below and see if you can move forward with it.

Syntax error:

// Syntax here would cause error
$q = new WP_Query(
array('posts_per_page' => 'posts_per_page,'post_type'=>array($post_type),'category_name'=>$category));

Fix to something like this

$q = new WP_Query(
array('posts_per_page' => $posts_per_page, 'post_type'=>array($post_type), 'category_name' => $category));

Unnecessary 4th argument:

// 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, '' );

Fix to something like this

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

My advice is to ensure each line of your code is valid in terms of syntax and the usage. Otherwise you will be lost in debugging process.

  • Thanks @Elisha Terada for you nice answer. The editor , who edit my post has made the syntax error. You can find the correct one to my comment. – Alamin Sep 2 '14 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.