我设法在我的自定义帖子类型上显示上一篇和下一篇帖子的缩略图,但我一直遇到这个错误
“注意:试图在C:\\xampp\\htdocs\\tba\\wp content\\themes\\artikulo uno\\single-news\\u press.php的第137行获取非对象的属性”
我试着研究它,但没有找到解决我问题的方法。这是我一直在研究的代码。
Show thumbnail of previous post
<?php
$prev_post = get_previous_post(true);
$prev_thumbnail = get_the_post_thumbnail ( $prev_post->ID, array(100, 100) );
previous_post_link( $prev_thumbnail . \'%link\', \'<span class="prev-link"> <span>Previous Post</span> <br/> %title </span>\' );
?>
Show thumbnail of next post
<?php
$next_post = get_previous_post(true);
$next_thumbnail = get_the_post_thumbnail ( $next_post->ID, array(100, 100) );
next_post_link( $next_thumbnail . \'%link\', \'<span class="next-link"><span>Next Post </span><br/> %title </span>\' );
?>
SO网友:cybmeta
该消息是一个常见的PHP错误,表示您试图在PHP对象不是PHP对象时使用类似PHP对象的变量。如果您阅读文档get_previous_post()
和get_next_post()
, 您可以看到,如果无法确定上一个/下一个post,这些函数将返回post对象或null/空字符串。因此,在尝试使用对象之前,需要检查对象是否存在:
$prev_post = get_previous_post(true);
if( is_object( $prev_post ) ) {
$prev_thumbnail = get_the_post_thumbnail ( $prev_post->ID, array(100, 100) );
previous_post_link( $prev_thumbnail . \'%link\', \'<span class="prev-link"> <span>Previous Post</span> <br/> %title </span>\' );
}
$next_post = get_next_post(true);
if( is_object( $next_post ) ) {
$next_thumbnail = get_the_post_thumbnail ( $next_post->ID, array(100, 100) );
next_post_link( $next_thumbnail . \'%link\', \'<span class="next-link"><span>Next Post </span><br/> %title </span>\' );
}