首页 > 解决方案 > wordpress - 如何在functions.php中获取当前术语

问题描述

我想获得函数的当前术语。但显然这个功能在functions.php. 有解决这个问题的想法吗?

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

标签: wordpress

解决方案


<?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 );
}

推荐阅读