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 »";
}
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
和来自短代码的输出应为return
ed,而不是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();