3

Here is my code:

<?php
$posttags = get_the_tags();
if ($posttags) {
   $tagstrings = array();
   foreach($posttags as $tag) {
      $tagstrings[] = '<a href="' . get_tag_link($tag->term_id) . '" class="tag-link-' . $tag->term_id . '">' . $tag->name . '</a>';
   }
   echo implode(', ', $tagstrings);
}

// For an extra touch, use this function instead of `implode` to a better formatted string
// It will return "A, B and C" instead of "A, B, C"
function array_to_string($array, $glue = ', ', $final_glue = ' and ') {
    if (1 == count($array)) {
        return $array[0];
    }
    $last_item = array_pop($array);
    return implode($glue, $array) . $final_glue . $last_item;
}
?>

The code puts a comma after tags in WP (except the last tag). I would like to change the color of commas. How can I do it?

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Pityer
  • 113
  • 1
  • 14

1 Answers1

2

You can use something like this:

$glue = '<span class="tagglue">,</span> ';

and use that in your implode() calls (either place in your snippet).

Then create a css declaration like:

.tagglue {color: blue;}

Implementation:

<?php
$posttags = get_the_tags();
if ($posttags) {
   $tagstrings = array();
   foreach($posttags as $tag) {
      $tagstrings[] = '<a href="' . get_tag_link($tag->term_id) . '" class="tag-link-' . $tag->term_id . '">' . $tag->name . '</a>';
   }
   echo array_to_string($tagstrings);
}

// For an extra touch, use this function instead of `implode` to a better formatted string
// It will return "A, B and C" instead of "A, B, C"
function array_to_string($array, $glue = '<span class="tagglue">, </span>', $final_glue = ' and ') {
    if (1 == count($array)) {
        return $array[0];
    }
    $last_item = array_pop($array);
    return implode($glue, $array) . $final_glue . $last_item;
}
?>

I'll take this change to link several related pages on StackOverflow (that don't offer coloration):

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • I changed my code, as you wrote, but nothing happened `function array_to_string($array, $glue = ', ', $final_glue = ' and ')` – Pityer Oct 10 '18 at 06:37
  • Please check the source code to see if the `` tags were generated as intended. Did you add the `.tagglue {color: blue;}` declaration to the stylesheet? Is the style declaration in the source code? – mickmackusa Oct 10 '18 at 06:39
  • It strange. I changed my code, but I can't see the alteration in source. The span class="tagglue" absolutely miss. – Pityer Oct 10 '18 at 06:52
  • 1
    How about clearing the cache? Hard refresh the page reload? Can you check the last_modified datetime on the file that you are editing? These little hiccups happen, continue to investigate and let me know if you get any clues. Is the function actually getting called? or is your file merely echoing the first portion of your posted snippet? Do you need: `echo array_to_string($tagstrings);` after your foreach() loop? – mickmackusa Oct 10 '18 at 06:53
  • I deleted the cache (ctrl+shift+r). My code is just such as above. – Pityer Oct 10 '18 at 08:27