我一直在开发短代码,从插件中提取自定义分类法的术语。它在WordPress 4.5.0之前运行良好,现在意识到它在4.5.0之后无法运行。
我用下面这样的旧方法提取这些术语。
Old Method
// Sermon Topics
$prefix_topic_terms= array();
if ( ! empty( $prefix_topic_terms ) ) {
$prefix_sermon_topic = get_terms(\'ctc_sermon_topic\', \'orderby=name&hide_empty=0\');
foreach ($prefix_sermon_topic as $category) {
$prefix_topic_terms[$prefix_sermon_topic->slug] = $prefix_sermon_topic->name;
}
}
New Method that didn\'t work
// Sermon Topics
$prefix_topic_terms = array();
if ( ! empty( $prefix_topic_terms ) ) {
$prefix_sermon_topic = get_terms( array(
\'taxonomy\' => \'ctc_sermon_topic\',
\'hide_empty\' => false,
\'orderby\' => \'name\',
\'hide_empty\' => true,
));
foreach ($prefix_sermon_topic as $category) {
$prefix_topic_terms[$category->slug] = $category->name;
}
}
var_dump($prefix_topic_terms);
显示错误无效分类错误,如下所示
object(WP_Error)#2577 (2) {
["errors"]=>
array(1) {
["invalid_taxonomy"]=>
array(1) {
[0]=>
string(17) "Invalid taxonomy."
}
}
["error_data"]=>
array(0) {
}
}
我做错了什么?
Edited使用以下代码获得成功
// Sermon Topics
$prefix_topic_terms = array();
$prefix_sermon_topic = get_terms( array(
\'taxonomy\' => \'ctc_sermon_topic\',
\'orderby\' => \'name\',
\'hide_empty\' => true,
));
if ( ! empty( $prefix_topic_terms ) && ! is_wp_error( $prefix_topic_terms ) ) {
foreach($prefix_sermon_topic as $category) {
$prefix_topic_terms [$category->slug] = $category->name;
}
}
如果我删除if条件,则上述代码有效。