I'm trying to use tags of the single post, as meta keywords.

I tried using this:

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

It works, but the output is:

<meta name="keywords" content="<a href="http://127.0.0.1/1/tag/aquaman/" rel="tag">aquaman</a>,<a href="http://127.0.0.1/1/tag/batman/" rel="tag">batman</a>,<a href="http://127.0.0.1/1/tag/wonder-woman/" rel="tag">wonder woman</a>"/>

Is it possible to remove the tags link/URL? And just the text/tag itself will appear?

up vote 5 down vote accepted

The code is tested and working fine.

Place this code

<?php
    $posttags = get_the_tags();

    if( ! empty( $posttags ) ) :
        $tags = array();

        foreach($posttags as $key => $value){
            $tags[] = $value->name;
        }

?>

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

<?php endif;?>

instead of

<meta name="keywords" content="<?php the_tags( '', ',', '' );?>test<?php }?>"/>
  • This is just exactly what ia needed. Thanks a lot! – Archangel17 Apr 17 '17 at 8:03

Use get_the_tag_list() instead of the_tags(), since the_tags() prints the results while get_the_tag_list() returns it.

See documentation: https://developer.wordpress.org/reference/functions/get_the_tag_list/

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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