开发者

Use post tags for meta keyword content

Seems this should b开发者_Python百科e 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('',' , '); ?>" />


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); ?>" />


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 . ' '; 
}
?>


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.


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" />


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; ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜