Forums

Remove a comma at the end (26 posts)

  1. binbin
    Member
    Posted 2 weeks ago #

    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;
    }
    ?>
  2. MichaelH
    moderator
    Posted 2 weeks ago #

    Change:

    return $stuff;

    to

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

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

  3. t31os_
    Member
    Posted 2 weeks ago #

    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();)

  4. binbin
    Member
    Posted 2 weeks ago #

    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...

  5. t31os_
    Member
    Posted 2 weeks ago #

    What's the error?

  6. binbin
    Member
    Posted 2 weeks ago #

    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)){
  7. t31os_
    Member
    Posted 2 weeks ago #

    Oh yes, i see the error now..

    Change that to..

    $the_tags = get_the_tags($post->ID) {
  8. binbin
    Member
    Posted 2 weeks ago #

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

  9. iridiax
    Member
    Posted 2 weeks ago #

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

  10. binbin
    Member
    Posted 2 weeks ago #

    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...

  11. t31os_
    Member
    Posted 2 weeks ago #

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

    $the_tags = get_the_tags($post->ID);
  12. binbin
    Member
    Posted 2 weeks ago #

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

  13. bedex78
    Member
    Posted 2 weeks ago #

    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.

  14. binbin
    Member
    Posted 2 weeks ago #

    No it doesn't work

  15. MichaelH
    moderator
    Posted 2 weeks ago #

    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.

  16. binbin
    Member
    Posted 2 weeks ago #

    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;
    }
    ?>
  17. MichaelH
    moderator
    Posted 2 weeks ago #

    Please paste ALL the code from your header.php at wordpress.pastebin.ca, and report the link back here. Thanks.

  18. binbin
    Member
    Posted 2 weeks ago #

  19. MichaelH
    moderator
    Posted 2 weeks ago #

    Please provide a link to your site to see the current code in action.

  20. binbin
    Member
    Posted 2 weeks ago #

    the website is not yet online, I work on it on local version with xampp

  21. MichaelH
    moderator
    Posted 2 weeks ago #

    And that header.php is working now except for the comma at the end?

  22. binbin
    Member
    Posted 2 weeks ago #

    As I explained in my first post everything is working, except the fact that it display a little comma at the end, after my last keyword, and to have a clean meta keyword I just want to remove this comma :)

  23. MichaelH
    moderator
    Posted 2 weeks ago #

    The reason I ask about it working is that your header.php pastebin code has this:

    <meta name="keywords" content="keyword1,keyword2,<?php extract_tags(); ?>"/>

    but your very first example doesn't show the keyword1,keyword2.

    And, if I'm reading it correctly, I don't know how that header is working because it seems to be missing a closing '}':

    section of header.php.

    <?php if ( is_home() ) { ?>
        <title>my title</title>
        <meta name="description" content="mydescription" />
        <meta name="keywords" content="key,words" />
    <?php } else if ( is_single() ) { ?>
        <title><?php the_title(); ?></title>
    
        <meta name="keywords" content="keyword1,keyword2,<?php echo extract_tags(); ?>"/>
    <?php } ?>

    functions.php

    function extract_tags() {
      global $post;
      $stuff ='';
      $tags = get_the_tags($post->ID);
      if ($tags) {
        foreach($tags as $tag) {
          $stuff.= $tag->name . ',';
        }
      }
    return substr($stuff,0,-1);
    }
  24. binbin
    Member
    Posted 2 weeks ago #

    I have tested the modification of code for functions you propose to me, unfortunately it doesn't work and worth, all tags in my keywords are not displayed...

  25. MichaelH
    moderator
    Posted 2 weeks ago #

    It works for me using the WordPress Default theme.

    In wp-content/themes/default/header.php adding this code:

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

    right after:

    <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

    and this to the wp-content/themes/default/functions.php

    function extract_tags() {
      global $post;
      $stuff ='';
      $tags = get_the_tags($post->ID);
      if ($tags) {
        foreach($tags as $tag) {
          $stuff.= $tag->name . ',';
        }
      }
    return substr($stuff,0,-1);
    }

    Oh well maybe someone else can spot the problem.

  26. iridiax
    Member
    Posted 2 weeks ago #

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

    Okay, maybe you mean search engines in other countries because the major ones in the US ignore them:

    http://googlewebmastercentral.blogspot.com/2009/09/google-does-not-use-keywords-meta-tag.html

    http://blog.searchenginewatch.com/091007-161534

Reply

You must log in to post.

About this Topic

Tags