0

I can't figure this out but I'm trying to echo the post Tags of the post in the <head> section. This code resides in the <head> section of the site.

Basically I want the post tags to appear between the quotes.

ados_setKeywords('');

I've tried using wp_get_post_tags():

ados_setKeywords('<?php echo wp_get_post_tags() ?>'); 

However this only gives me 'Array'. I need to either display the post tags or nothing.

1

wp_get_post_tags() returns an array. You cannot echo an array or all you get is "Array".

If this is the ados_setKeywords you mean, then you need to feed it a comma delimited string, not an array.

ados_setKeywords(implode(', ',wp_get_post_tags())); 
  • Thanks s_da_hum, your code did wonders. I'm still new so I can't upvote, but when I can I'll come back to do it (I apologize for that). – Liwii Oct 7 '13 at 14:11
0

edit: s_ha_dum bet me to it, so mark his answer as correct if you want to concatenate items by comma (or any other delimiter).

Although this is not the best way to go about it, (you should probably be using the action, add_action('wp_head', 'your_callback'); to attach content to the <head> section of your site, you can when working with wp_get_post_tags() do the following;

ados_setKeywords( 
    implode( ', ', wp_get_post_tags() )   
);

or loop through the array:

$tags = wp_get_post_tags();

$my_tags = '';

foreach( $tags as $tag ) {

  //conditionally process tags if you wish

  $my_tags .= $tag;

}

ados_setKeywords( $my_tags );
  • The OP was asking the question in reference to ados_setKeywords. I did not track down exactly what that function did in any detail but kind-of assumed it hooked into things properly. – s_ha_dum Oct 7 '13 at 16:04
0
add_action('wp_head','get_post_tags_for_head');

function get_post_tags_for_head(){
    global $post;
    $all_tags_posts = wp_get_post_tags($post->ID);
    if(!empty($all_tags_post)){

        foreach ($all_tags_posts as tags_post){

            echo tags_post->name;

            // do whatever you want here

        }
    }
}
0

Why are you even bothering with this? Search engines don't even pay attention to meta keywords and haven't done so in years (see Matt Cutts in 2009)

Still, if you are going to insist on this, here's a quick & dirty version.

<?php
    $tags = get_tags();
    $html = '<meta name="keywords" content="';
    foreach ( $tags as $tag ) {
        $tag_link = get_tag_link( $tag->term_id );
        $html .= "{$tag->name}, ";
    }
    $html .= '" />';
    echo $html;
?>

It's going to output something like this:

<meta name="keywords" content="tag1, tag2, tag3, " />

If you're hot to remove the extra ", " on the last tag, you'll need to throw a little more logic in. It's not going to harm anything but your aesthetic sense of how the code should be written and since it's not even relevant data, we can dispense with the artistry.

You should probably adapt Wordpresser's suggestion to wrap it in a function then hook it into the head.

  • Search engines ignore meta keywords but they are required by some accessibility standards. – s_ha_dum Oct 7 '13 at 16:01

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.