0

How to add Post Tags inside a new element in header.php?

I'm probably overlooking an obvious function in the WordPress Codex.

I have tried adding these to header.php:

<meta name="page-tags" content="<?php the_tags(); ?>">

=> produces some unwanted html (I do not want to change core in the wp-includes folder

and

<meta name="post-tags-here" content="<?php get_the_tag_list(); ?>" >

=> doesn't print

and

<meta name="post-tags-here" content="<?php tag_description(); ?>" >

=> doesn't print

Any clues?

1

Use get_the_tags() instead of the_tags(), get_the_tags() does the following for you:

Returns an array of objects, one object for each tag assigned to the post.

with which you can work with to achieve what you want. See the examples on the codex page for more information.

1
  • 1
    thank you! This one works: <meta name="print-post-tag" content="<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo $tag->name; } } ?> " >
    – hamburger
    Jan 30 '15 at 20:56
0

Your second attempt is close, but you need to echo the result of the function and you may want to provide it with some arguments. The below should output the list of tags space-separated:

<meta name="post-tags-here" content="<?php echo get_the_tag_list( '', ' ', '' ); ?>" >
3
  • Are sure this will get rid of the link, so the a tag? Because I don't think so, I'm relatively sure actually.
    – Nicolai
    Jan 29 '15 at 19:39
  • Unfortunately, this still ouputs the entire Anchor element tag as constructed in category-template.php.
    – hamburger
    Jan 30 '15 at 20:38
  • ah, correct. you'll need to go with @ialocin's answer Jan 30 '15 at 21:00

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.