admin管理员组

文章数量:1332881

I'm trying to see if a post (thru id) has a specified tag

    if has_tag( $tag = 'Cat', $post = $post_id ) {
    $taganimal = "Cat";
}

I tried this, but it just crashes the site .

Got idea from /, but can't get it to work..

I'm trying to see if a post (thru id) has a specified tag

    if has_tag( $tag = 'Cat', $post = $post_id ) {
    $taganimal = "Cat";
}

I tried this, but it just crashes the site .

Got idea from https://developer.wordpress/reference/functions/has_tag/, but can't get it to work..

Share Improve this question asked Jun 28, 2020 at 20:18 JoBeJoBe 1712 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You should wrap your condition with brackets according to PHP syntax:

if (has_tag('Cat', $post_id)) {
    $taganimal = "Cat";
}

If you need to check multiple tags you can use something like

foreach (['Cat', 'Dog'] as $animal) if (has_tag($animal, $post_id)) {
    $taganimal = $animal;
    break;
}

but it would find only the first tag listed in array in case the post has several of them.

本文标签: searchSee if a post has a specified tag