wordpress-如何在functions.php中获取当前术语

时间:2018-09-06 12:20:38

标签: wordpress

我想获得函数的当前术语。但显然此功能在functions.php中不起作用。有解决这个问题的主意吗?

$term = get_term_by('name', get_query_var('download_tag'), 'download_tag' );

1 个答案:

答案 0 :(得分:1)

<?php
/**
 * getCurrentTerm 
 * get the current term which archive is being displayed
 * good for categories, tags and custom taxonomies
 * @author Ohad Raz
 * @return current term object or false is not in a term archive.
 */
function getCurrentTerm(){
    if (!is_category() && !is_tag() && !is_tax())
        return false;
    $term_slug = get_query_var( 'term' );
    $taxonomyName = get_query_var( 'taxonomy' );
    return get_term_by( 'slug', $term_slug, $taxonomyName );
}