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) );  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行