• 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?

Viewing 6 replies - 1 through 6 (of 6 total)
  • <meta name="keywords" content="<?php
    	if ($posttags) {
    		foreach($posttags as $tag) {
    			echo $tag->name . ' ';
    		}
    	}
    ?>" />

    try this

    Thread Starter matthewpaul

    (@matthewpaul)

    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.

    try

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

    Thread Starter matthewpaul

    (@matthewpaul)

    Michael,

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

    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.

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Using Tags as Meta Keywords’ is closed to new replies.