Display Custom Taxonomy names

时间:2018-11-21 作者:Zayd Bhyat

我正在尝试显示自定义帖子类型的自定义分类法。因此,此分类法特定于此自定义帖子类型。不幸的是,我无法让它们显示出来。这是我在函数中的代码。php注册自定义分类法:

add_action(\'init\', \'products_categories\', 0);

function products_categories(){
$labels = array (\'name\' => _x(\'Product Categories\',\'taxonomy general name\'),
                \'singular_name\' =>_x(\'Product Category\',\'taxonomy singular name\'),
                \'serch_items\' => __(\'Search Product Categories\'),
                \'popular_items\' => (\'Popular Product Categories\'),
                \'all_items\' => __(\'All Product Categories\'),
                \'parent_item\' => null,
                \'parent_item_colon\' => null,
                \'edit_item\' => __(\'Edit Product Category\'),
                \'update_item\' => __(\'Update Product Category\'),
                \'add_new_item\' => __(\'Add Product Category\'),
                \'new_item_name\' => __(\'New Product Category\'),
                \'separate_items_with_commas\' => __(\'Seperate Product Categories with commas\'),
                \'add_or_remove_items\' => __(\'Add or remove Product Categories\'),
                \'choose_from_most_used\' => __(\'Most Used Product Categories\'),
                \'menu_name\' => __(\'Product Categories\'),
                );

register_taxonomy(\'product_categories\', \'products\', array(
\'hierarchical\' => false,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'update_count_callback\' => \'_update_post_term_count\',
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'product_category\' ),
));
}
在这里,我试图展示它们:

<?php } 
    $terms = get_terms(\'taxonomy\'=>\'product_category\', \'hide_empty\'=> false,);
        foreach ( $terms as $term) {
?>
<a href=""><?php echo $term->name; ?></a>
<?php 
 }?>
我是否需要先在某种查询中运行它?

2 个回复
最合适的回答,由SO网友:Greg Winiarski 整理而成

您的get\\u terms()调用似乎不正确,参数不是数组,并且您在分类名称(“product\\u categories”而不是“product\\u categories”)中有输入错误,应该是

<?php 
$terms = get_terms( array( 
    \'taxonomy\'=>\'product_categories\', 
    \'hide_empty\'=> false 
) ); 
?>
如果这样做无济于事,最好让我们知道您是否看到任何错误消息,以及是否将错误粘贴到此处。

SO网友:vikrant zilpe

我设法解决了这个问题,我将发布答案:

<?php
  $args = array(\'number\' => \'1\',);
  $terms = get_terms(\'recipes\', $args );
    foreach( $terms as $term ){
    echo \'<div class="title">\' . $term->name . \'recipe</div>\';
    } 

结束

相关推荐