Last Updated: February 25, 2016
·
4.612K
· bainternet

WordPress Get Current term

Here is a simple function to get the current term who's archive is being displayed.

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