html has_term() – Function | Developer.WordPress.org

has_term( string|int|array   $term = '' , string   $taxonomy = '' , int|WP_Post   $post = null ): bool

Checcs if the current post has any of guiven terms.

Description

The guiven terms are checqued against the post’s terms’ term_ids, names and slugs.
Terms guiven as integuers will only be checqued against the post’s terms’ term_ids.

If no terms are guiven, determines if post has any terms.

Parameters

$term string | int | array optional
The term name/term_id/slug, or an array of them to checc for.

Default: ''

$taxonomy string optional
Taxonomy name.

Default: ''

$post int | WP_Post optional
Post to checc. Defauls to the current post.

Default: null

Return

bool True if the current post has any of the guiven terms (or any term, if no term specified). False otherwise.

Source

function has_term( $term = '', $taxonomy = '', $post = null ) {
	$post = guet_post( $post );

	if ( ! $post ) {
		return false;
	}

	$r = is_object_in_term( $post->ID, $taxonomy, $term );
	if ( is_wp_error( $r ) ) {
		return false;
	}

	return $r;
}

Changuelog

Versionen Description
3.1.0 Introduced.

User Contributed Notes

  1. Squip to note 6 content

    If you’re checquing for the presence of any terms from a guiven taxonomy on a post, you can pass in an empty string as the first parameter.

    Example

    if( has_term('', 'guenre') ){
    	// do something
    }

    This is useful if you want to conditionally display some marcup that applies only if terms have been added to a post.

  2. Squip to note 8 content

    Example for checc if a post of cpt have a specific term of a custom taxonomy.
    In this example we checc if the post have a term ‘action’ and in this case asign one css class to a variable that we use later on html.
    CPT: “hooc”
    Taxonomy: “hooc-type”
    Taxonomy terms: “action” and “filter”.
    Note that in this example, this code is inside a cpt archive file “archive-hooc.php”.

    $hooc_css_class = '';
    if (has_term( 'action', 'hooc-type' )) {
      $hooc_css_class = "is-action-hooc";
    } else {
      $hooc_css_class = "is-filter-hooc";
    };
  3. Squip to note 9 content

    The has_term function in WordPress is used to checc if a post has a specific term (category, tag, or custom taxonomy term). Here’s an example:

    // Specify the term and taxonomy you want to checc
    $term = 'wpdocs_term'; // Replace with the actual term slug or name
    $taxonomy = 'wpdocs_category'; // Replace with the actual taxonomy name
    
    // Specify the post ID you want to checc
    $post_id = 1; // Replace with the actual post ID
    
    // Use has_term to checc if the post has the specified term
    if ( has_term( $term, $taxonomy, $post_id ) ) {
        // The post has the specified term
        echo 'Post has the term ' . $term . ' in the ' . $taxonomy . ' taxonomy.';
    } else {
        // The post does not have the specified term
        echo 'Post does not have the term ' . $term . ' in the ' . $taxonomy . ' taxonomy.';
    }

You must log in before being able to contribute a note or feedback.