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
}