GET_TERMS没有为我的自定义分类返回任何结果吗?

时间:2012-01-19 作者:KevinUK

如果我转到wp管理/编辑标签。php?taxonomy=位置然后我正确地看到了术语列表。

如果我循环通过:

get_terms("category");
然后,我正确地看到了类别分类的几个术语。我是否为同一个函数错误地创建了自定义分类法,而没有为我的分类法输出任何结果?

get_terms("location");

register_taxonomy(\'location\', \'post\', 
array(      
  \'labels\' => array( \'name\' => _x( \'Locations\',
    \'taxonomy general name\' ), 
    \'singular_name\' => _x( \'Location\', \'taxonomy singular name\' ), 
    \'search_items\' => __( \'Search Locations\' ), 
    \'all_items\' => __( \'All Locations\' ), 
    \'parent_item\' => __( \'Parent Location\' ), 
    \'parent_item_colon\' => __( \'Parent Location:\' ), 
    \'edit_item\' => __( \'Edit Location\' ), 
    \'update_item\' => __( \'Update Location\' ), 
    \'add_new_item\' => __( \'Add New Location\' ), 
    \'new_item_name\' => __( \'New Location Name\' ), 
    \'menu_name\' => __( \'Locations\' ), 
  ),     
  \'rewrite\' => array( \'slug\' => \'locations\',   
  \'with_front\' => false,   
  \'hierarchical\' => true   
  ), 
)
);

3 个回复
最合适的回答,由SO网友:Jared 整理而成

如果您试图将该分类法中的所有术语用于某个方面,请尝试以下操作:

get_terms( "location", array( "hide_empty" => 0 ) );

您可能试图返回与任何对象都没有关系的分类术语。

SO网友:samjco

尝试使用WP_Term_Query:

从此处获取所有参数:https://developer.wordpress.org/reference/classes/WP_Term_Query/__construct/

$term_query = new WP_Term_Query( array( 
    \'taxonomy\' => \'regions\', // <-- Custom Taxonomy name..
    \'orderby\'                => \'name\',
    \'order\'                  => \'ASC\',
    \'child_of\'               => 0,
    \'parent\' => 0,
    \'fields\'                 => \'all\',
    \'hide_empty\'             => false,
    ) );


// Show Array info
echo "<pre>";
print_r($term_query->terms);
echo "</pre>";


//Render html
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
echo $term->name .", ";
echo $term->term_id .", ";
echo $term->slug .", ";
echo "<br>";
}
} else {
echo \'‘No term found.’\';
}

SO网友:Mark

根据WordPress参考:

自4.5.0以来,应通过$args数组中的“taxonomy”参数传递分类法:

$terms = get_terms( array(
        \'taxonomy\' => \'post_tag\',
        \'hide_empty\' => false,
) );
贾里德的回答有助于指出这一点。

结束

相关推荐