doujia1904 2014-10-21 18:35
浏览 7

试图在wordpress中获取非对象错误

On the page with id 239 I am trying to hide a certain post-format from displaying on it, however I am getting this error on all pages:

"Trying to get property of non-object in C:\wamp\www\wordpress\wp-includes\query.php on line 4337"

This is the code I am using in the functions.php file:

function exclude_campaigns( $query ) {
     if( $query->is_main_query() && $query is_page(239) ) {
        $tax_query = array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 'post-format-link' ),
            'operator' => 'NOT IN',
        ) );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'exclude_campaigns' );
  • 写回答

1条回答 默认 最新

  • dqrqp8492 2014-10-21 19:05
    关注

    After removing the $query before the is_page check, it works on my example :(

    Anyway, the error indicates some problem with the is_page() function. You could try to replace that check:

    if( $query->is_main_query() && ! empty( $query->posts ) && $query->posts[0]->ID == 239 ) {
        // ...
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?