[英]php error using wordpress function get_terms
I'm setting a variable in my site this way: 我以这种方式在网站上设置了一个变量:
$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;
The problem is that I'm also getting this php error: 问题是我也遇到了这个php错误:
Warning: 警告:
Invalid argument supplied for foreach()
Location: 位置:
wp-includes/class-wp-term-query.php:373
Call Stack: 调用堆栈:
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)
Am I setting this the right way? 我设置正确的方法吗?
You can check for the error with is_wp_error()
, 您可以使用
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>';
}
About the error you told in comment, it's look like you don't run with a WordPress version under 4.5 ? 关于您在评论中告诉的错误,看来您未使用低于4.5的WordPress版本运行?
Prior to 4.5.0, the first parameter of get_terms() was a taxonomy or list of taxonomies:
在4.5.0之前,get_terms()的第一个参数是分类法或分类法列表:
$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:
从4.5.0开始,分类法应该通过$ args数组中的'taxonomy'参数传递:
$terms = get_terms( array( 'taxonomy' => 'post_tag', 'hide_empty' => false, ) );
About get_terms() 关于get_terms()
In your case, remove taxonomy
from $args and 在您的情况下,请从$ args中删除
taxonomy
,然后
$terms = get_terms('category', $args);
Provide the taxonomy
arg: 提供
taxonomy
arg:
$args = array(
'taxonomy' => 'your_taxonomy',
'parent' => 2818,
'hide_empty' => false
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.