• 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 10 replies - 16 through 25 (of 25 total)
  • Please paste ALL the code from your header.php at wordpress.pastebin.ca, and report the link back here. Thanks.

    Thread Starter binbin

    (@binbin)

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

    Thread Starter binbin

    (@binbin)

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

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

    Thread Starter binbin

    (@binbin)

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

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

    Thread Starter binbin

    (@binbin)

    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…

    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.

    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:

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

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

Viewing 10 replies - 16 through 25 (of 25 total)
  • The topic ‘Remove a comma at the end’ is closed to new replies.