Wordpress如何获取当前的分类术语?

时间:2019-11-01 15:33:37

标签: wordpress taxonomy

在单个自定义类型帖子的页面上,我正在尝试获取当前的分类法术语

$current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
print_r($current_term);

current_term,get_query_var('term')和get_query_var('taxonomy')不返回任何内容

更新:

$ term = get_queried_object(); print_r($ term); :

    WP_Post Object
(
    [ID] => 324
    [post_author] => 1
    [post_date] => 2019-10-17 14:18:37
    [post_date_gmt] => 2019-10-17 14:18:37
    [post_content] => The performance data 
    [post_title] => Diversified Fund
    [post_excerpt] => 
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] => 
    [post_name] => diversified-value-fund
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2019-10-31 20:44:43
    [post_modified_gmt] => 2019-10-31 20:44:43
    [post_content_filtered] => 
    [post_parent] => 0
    [guid] => http://hwcm.my/?post_type=funds&p=324
    [menu_order] => 1
    [post_type] => funds
    [post_mime_type] => 
    [comment_count] => 0
    [filter] => raw
)

2 个答案:

答案 0 :(得分:0)

您可以使用查询的对象

$term = get_queried_object();
print_r($term);

答案 1 :(得分:0)

找到了解决方法:

print_r(get_the_terms(get_the_ID(), 'fund_category'));

给予:

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 5
            [name] => US Equity
            [slug] => us-equity
            [term_group] => 0
            [term_taxonomy_id] => 5
            [taxonomy] => fund_category
            [description] => 
            [parent] => 0
            [count] => 6
            [filter] => raw
            [term_order] => 1
        )

)