get_terms

Home » Functions » get_terms

Function Name: get_terms

The get_terms function is a built-in WordPress function that retrieves the terms in a given taxonomy or list of taxonomies. A term is a grouping of similar items in WordPress, such as categories or tags.

The get_terms function can be used to retrieve a list of terms in a specific taxonomy or all taxonomies. This is especially useful for developers or website owners who want to display a list of terms in a particular taxonomy.

The get_terms function takes several arguments to help specify which terms to retrieve. These arguments include taxonomy, orderby, order, include, exclude, and more.

Here is an example usage code for the get_terms function:

$taxonomy = 'category';
$terms = get_terms( array(
    'taxonomy' => $taxonomy,
    'hide_empty' => false,
) );

if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
    echo '<ul>';
    foreach ( $terms as $term ) {
        echo '<li><a href="' . esc_url( get_term_link( $term ) ) . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
}

In the above example, we are retrieving a list of terms in the "category" taxonomy and displaying them as an unordered list. We are also using the "esc_url" function to sanitize the term link before displaying it.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.