获取类别帖子数量时未定义的偏移量和非对象

时间:2018-02-19 23:04:07

标签: wordpress

在标准菜单类( walker_nav_menu start_el )中,我添加了我的代码,其中显示了类别中的帖子数量:

$PostsCount= '';
if($item->object == 'category'){
    $cats = get_categories(array('include'=>$item->object_id));
    if($cats[0]->count>0) {
        $PostsCount='<div>'.$cats[0]->count.'</div>';
    }
}

$item_output .= $args->link_before . $title . $args->link_after . $PostsCount;

脚本工作并显示,但在PHP中查询2个错误:

注意:未定义的偏移量:0

注意:尝试获取非对象的属性

这一行:if($cats[0]->count>0) {

我无法理解为什么它不对...我怎样才能纠正这些错误?

- = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -

问题已解决。如果有人有兴趣,这是解决方案:

$countPostsCat = '';
if($item->object == 'category'){
    $cats = get_categories(array('include'=>$item->object_id));
    if ($cats) {
        $cats[0]->count>0;
        $countPostsCat='<count>'.$cats[0]->count.'</count>';
    }
}

0 个答案:

没有答案