Scott B
Scott B

Reputation: 40157

Use post tags for meta keyword content

Seems this should be super simple, but I can't find the right API function to make it work...

I'd like to use a posts tags to populate the keywords meta content...

<meta name="keywords" content="tags from post go here seperated by commas">

I've tried this but it creates a link list of each post tag...

<meta name="keywords" content="<?php echo the_tags('',' , '); ?>" />

Upvotes: 2

Views: 1724

Answers (5)

amercader
amercader

Reputation: 4540

Try something like:

<?php
  $postTags = get_the_tags();
  $tagNames = array();
  foreach($postTags as $tag) {
    $tagNames[] = $tag->name;
  }
?>

<meta name="keywords" content="<?php echo implode(",", $tagNames); ?>" />

Upvotes: 3

Oleksandr Knyga
Oleksandr Knyga

Reputation: 633

Oneline warriant for amercader version.

<?php if ( $postTags = get_the_tags() ) : $tagNames = array(); foreach($postTags as $tag) $tagNames[] = $tag->name; ?> <meta name="keywords" content="<?php echo implode($tagNames,","); ?>" /> <?php endif; ?>

Upvotes: 0

Agus Puryanto
Agus Puryanto

Reputation: 1359

You can try with this :

<meta name="keywords" content="<?php if(is_single()) {
        $metatags = get_the_tags($post->ID);
        foreach ($metatags as $tagpost) {
            $mymetatag = apply_filters('the_tags',$tagpost->name);
            $keyword = utf8_decode($mymetatag); // Your filters...
            echo $keyword.",";
        }
    }
    ?>your,key,words" />

Upvotes: 0

jackbot
jackbot

Reputation: 3021

the_tags() automatically displays a link to each post tag. You could use get_the_tags() which returns an array of tag objects, which you can then loop through and get the name of the tag.

Upvotes: 0

Jon Gauthier
Jon Gauthier

Reputation: 25572

You need to use the template function get_the_tags to fetch the data instead of letting WordPress output it for you. You can then loop through this array and output the list however you would like:

<?php
if ( $posttags = get_the_tags() ) {
    foreach($posttags as $tag)
        echo $tag->name . ' '; 
}
?>

Upvotes: 1

Related Questions