2016-11-25 48 views
1

我这样设定的变量在我的网站:PHP错误get_terms

$args = array('parent' => 2818,'hide_empty' => false); 
$best_of_cat_child_terms = get_terms($args); -> (functions.php:26) 
$best_of_cat = $best_of_cat_child_terms; 

的问题是,我也越来越这个PHP错误:

警告:

Invalid argument supplied for foreach() 

地点:

wp-includes/class-wp-term-query.php:373 

调用堆栈:

WP_Term_Query->get_terms() 
wp-includes/class-wp-term-query.php:287 
WP_Term_Query->query() 
wp-includes/taxonomy.php:1217 
get_terms() 
wp-content/themes/theme-child/functions.php:26 (-> functions.php line 26 marked above) 

难道我设置这个正确的方式?

+0

什么WP版本? –

+0

@cale_b版本4.6.1 – Alex

回答

0

提供的taxonomy ARG:

$args = array(
    'taxonomy' => 'your_taxonomy', 
    'parent' => 2818, 
    'hide_empty' => false 
); 
+0

如果我将分类树arg设置为“类别”,变量停止工作 – Alex

+1

该变量停止工作?这是什么意思? –

3

您可以检查与is_wp_error()错误,

$terms = get_terms(array(
     'taxonomy'=> 'category', 
     'parent' => 2818, 
     'hide_empty' => 0) 
); 

if (! empty($terms) && ! is_wp_error($terms)){ 
    echo '<ul>'; 
    foreach ($terms as $term) { 
     echo '<li>' . $term->name . '</li>'; 
    } 
    echo '</ul>'; 
} 
else{ 
    $error_string = $terms->get_error_message(); 
    echo '<div id="message" class="error"><p>' . $error_string .'</p></div>'; 
} 

关于你的意见告诉了错误,它看起来像你不要用跑WordPress版本在4.5以下?

Prior to 4.5.0, the first parameter of get_terms() was a taxonomy or list of taxonomies:

$terms = get_terms('post_tag', array(
'hide_empty' => false, 
)); 

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:

$terms = get_terms(array(
'taxonomy' => 'post_tag', 
'hide_empty' => false, 
)); 

关于get_terms()

在你的情况下,删除taxonomy从$指定参数和

$terms = get_terms('category', $args);