• Hello,

    I use a function to extract my tag of post on single page to put them in my meta keywords, unfortunately, this function create an undesired comma at the end(see after tag4), so I have a result like on this example :

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

    And I want a result like this :

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

    Someone could tell me what I need to change in my function to have this result please ?

    Here’s my function :

    <?php
    function extract_tags() {
    global $post;
    $the_tags ='';
    if(get_the_tags($post->ID)){
    foreach(get_the_tags($post->ID) as $tag) {
    echo $stuff[] = $tag->name . ',';
    }
    }
    return $stuff;
    }
    ?>
Viewing 15 replies - 1 through 15 (of 25 total)
  • Change:

    return $stuff;

    to

    return substr($stuff,0,-1);

    http://php.net/manual/en/function.substr.php

    Or, implode the array…

    <?php
    function extract_tags() {
    	global $post;
    	$the_tags = array();
    	$the_tags = get_the_tags($post->ID)){
    	if(!$the_tags || !is_array($the_tags)) return;
    	foreach($the_tags as $tag) {
    		$tags[] = $tag->name;
    	}
    	unset($the_tags);
    	return implode(',',$tags);
    }
    ?>

    Just remember to echo the function.. (ie. echo extract_tags();)

    Thread Starter binbin

    (@binbin)

    Humm 🙁

    @michaelh : unfortunately it change anything the comma is always here…

    @t31os_ : it return me an error on the line 5, so it’s doesn’t works too…

    What’s the error?

    Thread Starter binbin

    (@binbin)

    Parse error: syntax error, unexpected ‘)’ in D:\Program\xampp\htdocs\wp-content\themes\mytheme\functions.php on line 30

    the line 30 correspond to the line five of the extract tag function :

    $the_tags = get_the_tags($post->ID)){

    Oh yes, i see the error now..

    Change that to..

    $the_tags = get_the_tags($post->ID) {

    Thread Starter binbin

    (@binbin)

    Parse error: syntax error, unexpected ‘{‘ in D:\Program\xampp\htdocs\wp-content\themes\mytheme\functions.php on line 30

    Meta keywords are now totally ignored by search engines, so I wouldn’t bother with them.

    Thread Starter binbin

    (@binbin)

    Meta keywords is not so important than in the past but there’s are absolutely not ignored by search engines.

    So please next time don’t loose your time to make this type of answer iridiax…

    lol, i really don’t have my head screwed on today, that line should have been.

    $the_tags = get_the_tags($post->ID);

    Thread Starter binbin

    (@binbin)

    There’s no error this time, but this function doesn’t work, it display anything, no tags in my meta keywords

    I found a trick to do that but it was used for a string… Not sure if it would work for arrays, though…

    Here’s the code:

    $stuff = substr_replace($stuff,"",strrpos($stuff,","));

    Place it before:

    return $stuff;

    Just play around with it as I’m not sure if it’d work.

    Thread Starter binbin

    (@binbin)

    No it doesn’t work

    Please paste all the code from the theme template file (probably index.php) that is displaying those posts into a pastebin, such as wordpress.pastebin.ca, and report the link back here. Maybe someone can spot your problem. Thanks.

    Thread Starter binbin

    (@binbin)

    Here’s the code…

    In my header.php :

    <?php if ( is_single() ) { ?>
    <meta name="keywords" content="<?php extract_tags(); ?>"/>

    In my function.php :

    <?php
    function extract_tags() {
    global $post;
    $the_tags ='';
    if(get_the_tags($post->ID)){
    foreach(get_the_tags($post->ID) as $tag) {
    echo $stuff[] = $tag->name . ',';
    }
    }
    return $stuff;
    }
    ?>
Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Remove a comma at the end’ is closed to new replies.