Target posts that only have 1 specific tag
-
Hi
I’m using
if (has_tag('tag-name')
to target posts that have a certain tag (amongst other tags).I’d like to do something different on posts that are only tagged with that specific tag though and have no other tags. At the moment, the above code includes these posts, obviously.
So is it possible to target post with only 1 specific tag? Could you count the tags and then check if it’s the specific tag I want to target?
Thanks
-
How about something like:
<?php $tags = get_the_tags(); if ( has_tag( 'tag-name' ) && 1 == count( $tags ) ) : ?> ... do something ... <?php endif; ?>
Many thanks, I think this could work.
I’ve been thinking about what I’m trying to do. Is it possible to extend this with some elseif magic?
I know the following is incorrect but something like…
<?php $tags = get_the_tags(); if ( has_tag( 'tag-name' ) && 2 == count( $tags ) ) : ?> ... do something ... <?php elseif; ?> $tags = get_the_tags(); if ( has_tag( 'tag-name' ) && 1 == count( $tags ) ) : ?> echo 'Text'; <?php elseif; ?> $tags = get_the_tags(); if ( !has_tag( 'tag-name' ) && 1 == count( $tags ) ) : ?> ... do something ... <?php endif; ?>
I want something that says…
If the post has 2 tags (and one of them is the one specified) then display the 2nd tag.
If the post has 1 tag (and it is the one specified) then display some text.
If the post has 1 tag (and it is NOT the one specified) then display the tag.
This sort of does it but only seems to make the first if statement work.
<?php $posttags = get_the_tags(); $count=0; if ($posttags) { foreach($posttags as $tag) { $count++; if (!has_tag('tag-name') && (1 == $count)) { echo $tag->name . ' '; } }} elseif ($posttags) { foreach($posttags as $tag) { $count++; if (has_tag('tag-name') && (1 == $count)) { echo 'Text'; } }} elseif ($posttags) { foreach($posttags as $tag) { $count++; if (has_tag('tag-name') && (2 == $count)) { echo $tag->name . ' '; } }} else { echo 'Text'; } ?>
You were actually pretty close on your first attempt. The reason your second attempt wasn’t working very well is because
get_the_tags()
only returns false if the post has no tags, meaning that you could never progress past the firstif
.Try something like this:
<?php $tags = get_the_tags(); ?> <?php if ( ! $tags ) : ?> <p>No tags.</p> <?php elseif ( has_tag( 'tag-one' ) && 2 == count( $tags ) ) : ?> <?php foreach ( $tags as $single_tag ) { ?> <p>This post has exactly two tags, and one of them is <?php echo $single_tag->name; ?>!</p> <?php } ?> <?php elseif ( has_tag( 'tag-one' ) && 1 == count( $tags ) ) : ?> <?php foreach ( $tags as $single_tag ) { ?> <p>This post has exactly one tag, and it's <?php echo $single_tag->name; ?>!</p> <?php } ?> <?php elseif ( ! has_tag( 'tag-one' ) && 1 == count( $tags ) ) : ?> <?php foreach ( $tags as $single_tag ) { ?> <p>This post has exactly one tag, and it's not tag-one, it's <?php echo $single_tag->name; ?>!</p> <?php } ?> <?php else : ?> <p>Something happened. Tags: <?php echo ( $tags ); ?></p> <?php endif; ?>
Here’s another idea:
<?php $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'names' ) ); ?> <?php $tag_exists = in_array( 'tag-one', $tags ); ?> <?php if ( $tag_exists && 2 == count( $tags ) ) : ?> <p>This post has exactly two tags, and one of them is tag-one!</p> <?php elseif ( $tag_exists && 1 == count( $tags ) ) : ?> <p>This post has exactly one tag, and it's tag-one!</p> <?php elseif ( ! $tag_exists && 1 == count( $tags ) ) : ?> <p>This post has exactly one tag, and it's not tag-one, it's <?php echo $tags[0]; ?>!</p> <?php else : ?> <p>This post either has three or more tags, or no tags at all, or it has one tag that wasn't the right one, or it has two tags, neither of which were the right one.</p> <?php endif; ?>
Ah, that second solution sounds like it might be the one.
How do I display the tags using that one?
Would I replace
<p>This post has exactly two tags, and one of them is tag-one!</p>
with
<?php echo $tags; ?>
Close. Since
$tags
is an array, you need to do<?php echo $tags[0]; ?>
. Or, in the case of two tags, you need to do<?php foreach ( $tags as $singletag ) { echo $singletag . ' '; } ?>
.Ah, OK.
Not sure the code is working correctly.
Just testing and everything is displaying the last part
<p>This post either has three or more tags, or no tags at all, or it has one tag that wasn't the right one, or it has two tags, neither of which were the right one.</p>
I’ve changed the part at the beginning to be my specific tag
<?php $tag_exists = in_array( 'tag-one', $tags ); ?>
Am I missing anything else?
One thing to check is that
in_array()
is case-sensitive: “Tag” won’t match “tag”. If that’s not the problem, could you post the complete code here or at pastebin?My tag is numerical (1882) – will that effect it?
I’m basically just using your code right now to try and get it working. So it’s this…
<?php $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'names' ) ); ?> <?php $tag_exists = in_array( '1882', $tags ); ?> <?php if ( $tag_exists && 2 == count( $tags ) ) : ?> <p>This post has exactly two tags, and one of them is tag-one!</p> <?php elseif ( $tag_exists && 1 == count( $tags ) ) : ?> <p>This post has exactly one tag, and it's tag-one!</p> <?php elseif ( ! $tag_exists && 1 == count( $tags ) ) : ?> <p>This post has exactly one tag, and it's not tag-one, it's <?php echo $tags[0]; ?>!</p> <?php else : ?> <p>This post either has three or more tags, or no tags at all, or it has one tag that wasn't the right one, or it has two tags, neither of which were the right one.</p> <?php endif; ?>
But it’s just always displaying the last <p> on every post.
Also, I’m not sure if it does do everything I need. I need one that says <p>This post has two tags. One of them is 1882 but display the other tag.</p>
Sorry to be a pain! Many thanks for helping.
Could you add
var_dump( $tags );
after the first line of that code and post the output? I want to see ifwp_get_post_tags()
is returning the right values.And for your second question, do you need to display both tags, just the tag 1882, or just the tag that isn’t 1882?
OK, so I changed the first line to be
<?php $tags = wp_get_post_tags( $post->ID, array( ‘fields’ => ‘names’ ) ); var_dump( $tags );?>
The output is array(0) { } followed by the final <p>
Just the tag that isn’t 1882 please.
It looks like
$post
isn’t set correctly. Are you running this code within the loop or infunctions.php
or somewhere else?For your second question, try this code:
<?php if ( $tag_exists && 2 == count( $tags ) ) : ?> <?php foreach( $tags as $key => $value ) { if ( $tags[$key] == '1882' ) : unset( $tags[$key] ); break; endif; } $tags = array_values( $tags ); ?> <?php echo ( $tags[0] ); ?> <?php elseif ( $tag_exists && 1 == count( $tags ) ) : ?> ... the rest of the code...
unset()
removes the value “1882”,array_values()
reorganizes the array, andecho ( $tags[0] )
echoes the remaining value.Ah, it’s in my functions.php file.
Using a hook to put this code into a template file.
Aha. You need to have
global $post;
within your custom function so it can access the$post
variable from the loop.
- The topic ‘Target posts that only have 1 specific tag’ is closed to new replies.