Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to if the check next post has a specific tag. I tried the following code, but it is not working because has_tag() only works in the loop. I want to use it outside the loop.

<?php if ( has_tag( 'mario', $post->ID ) ) : ?>
my content
<?php endif; ?>

The following code lists the tags from post, but I don't know how to make it work with an if() / else() condition:

global $post;
foreach ( get_the_tags( $post->ID ) as $tag ) {
    echo $tag->name . ', ';
}
share|improve this question
1  
What is the context? Why are your trying to do this? What is the result you want? –  s_ha_dum Dec 1 '12 at 14:41

2 Answers 2

The has_tag() conditional does not have to be used inside the Loop; it can be passed a $post object as a second parameter:

has_tag( $tag, $post );

Since has_tag() defaults to the current post, you simply need to pass it the object for the next adjacent post. Fortunately, WordPress provides a function to retrieve adjacent posts: get_adjacent_post():

get_adjacent_post( $in_same_cat, $excluded_categories, $previous );

The $in_same_cat parameter defaults to false, the $excluded_categories parameter defaults to '', and the $previous parameter defaults to true. So, we just need to change that third parameter to false, to retrieve the next post, instead of the previous post:

get_adjacent_post( false, '', true );

Combining it with your has_tag() conditional:

if ( has_tag( 'mario', get_adjacent_post( false, '', true ) ) {
    // Next post has the 'mario' post tag;
    // do something
}
share|improve this answer
    
yes, you're right about has_tag. I was wrong. Its a simple solution you've mentioned above. my wrong information was the only problem. otherwise I already was using get_adjacent_post to get next post id. Thanks you –  wp student Dec 2 '12 at 20:15
    
If Chip's answer worked for you, then please mark it as correct using the green tick next to the left of the post. Thanks –  bungeshea_ Dec 22 '12 at 0:37

You should be able to use something like this:

$post_id = 123;
$posttags = get_the_tags($post_id);
if (!is_wp_error($posttags) && $posttags) {
    foreach($posttags as $tag) {
        if ($tag->name == 'mario') {
            echo 'yup';
        }
    }
}

What you're doing is:

  • Grabbing the tags
  • If there are any tags, running each of them through a foreach
  • If one of those foreach instances match the case of mario, doing something (in this case, echoing 'yup')

Let me know if that helps.

share|improve this answer
    
thanks, its working. but at mario page its printing both yup and nope. at other pages it is showing "nope" only, which means its correct here. –  wp student Dec 1 '12 at 13:57
    
actually when I use for current post id. it works fine. the problem is I want to use it for $next_post->ID at mario page it prints both yup and nope –  wp student Dec 1 '12 at 14:14
    
$posttags = get_the_tags($next_post->id); if (!is_wp_error($posttags) && $posttags) { foreach($posttags as $tag) { if ($tag->name == 'mario') { echo 'yup'; } elseif($tag->name !== 'mario') { echo 'nope'; } } } I've tried this one too, but still its printing "yup nope" at post having mario tag. where as at pages with other tags, it prints nope only, which is fine. The problem is occurring only on mario tagged posts. –  wp student Dec 1 '12 at 21:01
    
It runs through each available tag for the post, so if mario is available, then it would print yup in addition to nope if any others available in the post do not match. So - if you don't want anything to print on tags that don't match mario, then just take out the else statement (revised above) –  Zach Dec 2 '12 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.