WordPress.org

Ready to get started?Download WordPress

Forums

Using Tags as Meta Keywords (7 posts)

  1. matthewpaul
    Member
    Posted 4 years ago #

    I'm using this code to have tags as meta keywords:

    <meta name="keywords" content="<?php
    	if ($posttags) {
    		foreach($posttags as $tag) {
    			echo $tag->name . ', ';
    		}
    	}
    ?>" />

    It's adding a comma as a separator between each keyword. How can I remove the comma that shows up after the last keyword?

  2. refulez
    Member
    Posted 4 years ago #

    <meta name="keywords" content="<?php
    	if ($posttags) {
    		foreach($posttags as $tag) {
    			echo $tag->name . ' ';
    		}
    	}
    ?>" />

    try this

  3. matthewpaul
    Member
    Posted 4 years ago #

    I want to keep the comma so that it properly separates the keywords in the meta tag. Just want to remove the comma from showing up after the last keyword.

  4. MichaelH
    Member
    Posted 4 years ago #

    try

    <meta name="keywords" content="<?php
    if ($posttags) {
      $tags='';
      foreach($posttags as $tag) {
        $tags .= $tag->name . ', ';
      }
      echo substr($tags,0,-2);
    }
    ?>" />
  5. matthewpaul
    Member
    Posted 4 years ago #

    Michael,

    I tried replacing my code with yours, but no tags are showing up in the output.

  6. MichaelH
    Member
    Posted 4 years ago #

    Hmm...don't see a problem, so put you whole template in wordpress.pastebin.com and report the link back here and maybe someone can spot the problem.

  7. skywolfyx1
    Member
    Posted 4 years ago #

    paste function.php file

    function csv_tags() {
    	$posttags = get_the_tags();
    	foreach((array)$posttags as $tag) {
    		$csv_tags .= $tag->name . ',';
    	}
    if (is_home()) { ?>keyword1,keyword2,<?php }
    	echo "$csv_tags";
    
    }

    paste header.php between <head> </head
    <meta name="keywords" content="<?php echo csv_tags(); ?>" />

Topic Closed

This topic has been closed to new replies.

About this Topic