2016-11-25 50 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);