我如何列出所有的鼻涕虫类别?

时间:2018-08-16 作者:martins

我需要列出taxonomy portfolio\\u类别中的所有slug类别。像这样:

the_terms( get_the_ID(), \'portfolio_category\', \'\', \', \' );
这将返回单个投资组合中的类别。我只想把这一点点还给你。

3 个回复
SO网友:1naveengiri

您可以尝试以下代码

$term = get_term( get_the_ID(), \'portfolio_category\');
$slug = $term->slug;
echo  $slug;

SO网友:nmr

Get terms attached to the post

$post_terms = get_the_terms(get_the_ID(), \'portfolio_category\');
if( is_array($post_terms) ) {
    $post_terms = array_reduce($post_terms, function($carry, $item){
            $carry[] = $item->slug;
            return $carry;
        }, []);
    echo implode(\', \', $post_terms);
}
<小时>Get all terms

要从给定的分类中检索所有术语,请使用函数get_terms. Here 您将找到有关已接受参数的信息。

自WP 4.5起:

$portfolio_terms = get_terms([
    \'taxonomy\' => \'portfolio_category\', 
    \'hide_empty\' => false,
    \'fields\' => \'id=>slug\',
    \'orderby\' => \'slug\',
]);
if( is_array($portfolio_terms) )
    echo implode(\', \', $portfolio_terms);
WP 4.5之前:

$portfolio_terms = get_terms(\'portfolio_category\', [
    \'hide_empty\' => false,
    \'fields\' => \'id=>slug\',
    \'orderby\' => \'slug\',
]);
if( is_array($portfolio_terms) )
    echo implode(\', \', $portfolio_terms);

SO网友:martins

I got. I use this:

$post_id = get_the_ID();

$term = get_the_terms( get_the_ID(), \'portfolio_category\');
foreach($term as $simples){
    echo $simples->slug;

}
结束

相关推荐

Media Library Categories

我使用以下内容将类别分配给我的WordPress媒体库(在functions.php):function wptp_add_categories_to_attachments() { register_taxonomy_for_object_type( \'category\', \'attachment\' ); } add_action( \'init\' , \'wptp_add_categories_to_attachments\' ); 每个媒体库项目都