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
(seeWP_Query
). The reason is, atax_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="https://wordpress.stackexchange.com/questions/159818/<?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="https://wordpress.stackexchange.com/questions/159818/<?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 »";
}
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 return
ed, 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();