Failed to get get_the_tags () in wordpress template
I am using this code to get the tags in my wordpress posts for a theme
`<?php
$posttags = get_the_tags();
if ($posttags) {
foreach ($posttags as $tag) {
$tagnames[count($tagnames)] = $tag->name;
}
$comma_separated_tagnames = implode(", ", $tagnames);
print_r($comma_separated_tagnames);
}
?>`
The PROBLEM is that it returns tags for "all posts" not only for individual posts, and I think the problem is if the DOESNT post has tags - it just inserts the tags anyway.
Can anyone help change it like this:
- It only returns tags for non-tagged posts
- If there are no tags for the post, do not return anything
<footer class="entry-footer">
<?php //get all tags for the post
$t = wp_get_post_tags($post->ID);
echo "<p class='tags-list'>TAGGED WITH: ";
foreach ($t as $tag) {
$tag_link = get_tag_link($tag->term_id);
echo "<a href='$tag_link' class='used-tag' rel='tag'>".($tag->name)."</a> ";
}
echo "</p>";
?>
</footer>
This is what I did, it displays the tags for each post in the loop.
a source to share
ok i hope this can help someone, i got stuck on this question for about an hour trying to get my post tags "so i can mix it with the twitter link" the_tags (); the function was useless as I am using it outside of the WP loop, get_the_tag_list (); were perfect for me as it can include the post id,
$postid = $wp_query->post->ID;
$posttags = strip_tags( get_the_tag_list( ' ', '', '', "$postid" ) ) ;
^^ In the above code, I removed the html codes to get the tag name without the href link.
this is a use case for the function: -
get_the_tag_list( string $before = '', string $sep = '', string $after = '', int $id )
a source to share