0

I would like to have on my single.php:

Tag: Tag1

Or if more than one Tag:

Tags: Tag1, Tag2

The Tag1 and Tag2 should link to a custom URL.

I found this, but don't know how to get exactly what I want.

if( has_tag() ) {
    // somehow echo "Tags" + Tagnames with custom links seperated by commas.
}
else{
        // show nothing
    }
  • Your question is not clear. Add some code and more description to make it more understandable. Otherwise it's hard for us to get the real scenario. – CodeMascot Feb 5 '17 at 13:52
0

you can use the get_the_tags function, something along the lines of:

$posttags = get_the_tags();
if ($posttags) {
    echo (count($posttags) > 1) ? 'Tags: ' : 'Tag: ';
    $links = array();
    foreach($posttags as $tag) {
        $links[] = sprintf('<a href="%s">%s</a>',  get_tag_link($tag->term_id), $tag->name);
    }
    echo implode(', ', $links);
}

Reference: https://codex.wordpress.org/Function_Reference/get_the_tags

  • This works perfectly! Thanks. - Would like to have separators like a comma between multiple tags. How do I do that? – JohnMcRow Feb 5 '17 at 22:06
  • Just modify the sprintf template to '<a href="%s">%s</a>, ' – Ignacio Jose Canó Cabral Feb 6 '17 at 14:02
  • Thanks! But if there is only one tag, I'll get "Tag1," - with many tags this is fine. – JohnMcRow Feb 6 '17 at 16:19
  • You're right, i'll update answer – Ignacio Jose Canó Cabral Feb 6 '17 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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