2016-08-05 34 views
0

我使用分页下面的代码,它工作正常,但抛出一个PHP的警告:非法抵消未设置类型(unset($allcategories[$parentc]);)WordPress的PHP的警告:非法偏移类型在未设置

不要有很多PHP的经验,但你们可以指导我的问题在哪里?提前致谢。

<nav class="navigation post-navigation" role="navigation"> 
<h1 class="screen-reader-text"><?php _e('Post navigation', 'ci'); ?></h1> 

<?php 
$allcategories = get_terms(
    array('category'), // Taxonomies 
    array('fields' => 'ids') // Fields 

); 

$post_categories = wp_get_post_categories($post->ID); 

foreach ($post_categories as $pc) { 
if(($key = array_search($pc,$allcategories)) !== false) { 
    unset($allcategories[$key]); 
} 
$this_category = get_category($pc); 
if ($this_category->category_parent != '') { 
    $parentc = get_category($this_category->category_parent); 
    unset($allcategories[$parentc]); 
} 

} 

?> 
<div class="nav-links"> 
    <div class="nav-next"><?php next_post_link('%link', '%title', FALSE, $allcategories); ?></div> 
    <div class="nav-previous"><?php previous_post_link('%link', '%title', FALSE, $allcategories); ?></div> 

</div> 
</nav> 
+0

检查你在'$ parentc'中得到了什么? – devpro

+0

WP_Term对象 ( [term_id] => 4 [名称] => DFW地铁 [蛞蝓] =>达拉斯堡垒价值 [term_group] => 0 [term_taxonomy_id] => 4 [分类] = > 类别[描述] => [父] => 0 [数] => 4883 [过滤] =>生 [term_icon] => [CAT_ID] => 4 [category_count] => 4883 [category_description] => [cat_name] => DFW地铁 [category_nicename] =>达拉斯堡值 [category_parent] => 0 ) –

+0

这只是一个对象记录 –

回答

2

当你调用

$parentc = get_category($this_category->category_parent); 

get_category()函数返回一个对象。对象不允许作为数组的键。你可能正在做...

unset($allcategories[$parentc->term_id]); 
+0

我也推荐调试像'print_r($ parentc);' – devpro

+0

我看,谢谢你的详细解释。 –

相关问题