dqwh1218 2019-05-29 16:37
浏览 40
已采纳

如何在上一篇文章缩略图中修复“试图获取非对象的属性”通知

I added a thumbnail for next and previous post in my Wordpress theme specifically inside the single.php file.

It works as require but it is displaying the notice:

Notice: Trying to get property of non-object in \wp-content\themes\theme\template-parts\content-footer.php on line 27

I have tried out some of the examples from similar answers in this website but they do not work for me. I removed both $prevPost->ID and $nextPost->ID but it then displays the current post thumbnail instead.

The code causing the error is below: The line of code causing the notice is the 3rd and 4th lines below:

<?php 
    $prevPost = get_previous_post(); 
    $nextPost = get_next_post(); 
    $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(50,50) );  
    $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(50,50) );  
?>

The thumbnails were called using the codes below:

<div class="uk-width-auto"><?php echo $prevthumbnail; ?></div> and <div class="uk-width-auto"><?php echo $nextthumbnail; ?></div> which both works.

The error is just a notice so it does not break the website or even appear unless wordpress debug is enabled. However, I would prefer not getting this notice so as not to cause some concern for my client.

Any idea on how to resolve this?

  • 写回答

1条回答 默认 最新

  • dqjjw04440 2019-05-29 16:38
    关注

    So, in WordPress - the first post in the database is NOT going to have a "previous" post, and the last post is not going to have a "next" - so this behavior is perfectly normal.

    To prevent the notice, you just need to check if it exists first - I typically like to use empty to perform the check - like so:

    $prevPost = get_previous_post(); 
    $nextPost = get_next_post(); 
    if ( ! empty( $prevPost->ID ) ) {
        $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(50,50) );  
    }
    
    if ( ! empty( $nextPost->ID ) ) {
        $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(50,50) );  
    }
    

    Note that this may have the undesirable effect of leaving $nextthumbnail and / or $prevthumbnail as undefined variables, so to combat that, I'd recommend modifying the code even further:

    $prevPost = get_previous_post(); 
    $nextPost = get_next_post(); 
    // use a ternary to set the thumbnail if not empty, or empty string if empty
    $prevthumbnail = ( empty( $prevPost->ID ) ) ? '' : get_the_post_thumbnail($prevPost->ID, array(50,50) );  
    // use a ternary to set the thumbnail if not empty, or empty string if empty
    $nextthumbnail = ( empty( $nextPost->ID ) ) ? '' : get_the_post_thumbnail($nextPost->ID, array(50,50) );  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表