更改 php 中逗号的颜色
Change the color of commas in php
这是我的代码:
<?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;
}
?>
代码在 WP 中的标签后放置一个逗号(最后一个标签除外)。我想改变逗号的颜色。我该怎么做?
你可以这样使用:
$glue = '<span class="tagglue">,</span> ';
并在您的 implode()
调用中使用它(在您的代码段中的任一位置)。
然后创建一个 css 声明,例如:
.tagglue {color: blue;}
实施:
<?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;
}
?>
我会将此更改应用于 link Whosebug 上的几个相关页面(不提供着色):
- Implode array with ", " and add "and " before last item
- Imploding with "and" in the end?
- implode() an array with multiple database columns, except on last entry PHP
- Comma separated list from array with "and" before last element
- PHP Add commas to items with AND
- Replace the last comma with an & sign
这是我的代码:
<?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;
}
?>
代码在 WP 中的标签后放置一个逗号(最后一个标签除外)。我想改变逗号的颜色。我该怎么做?
你可以这样使用:
$glue = '<span class="tagglue">,</span> ';
并在您的 implode()
调用中使用它(在您的代码段中的任一位置)。
然后创建一个 css 声明,例如:
.tagglue {color: blue;}
实施:
<?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;
}
?>
我会将此更改应用于 link Whosebug 上的几个相关页面(不提供着色):
- Implode array with ", " and add "and " before last item
- Imploding with "and" in the end?
- implode() an array with multiple database columns, except on last entry PHP
- Comma separated list from array with "and" before last element
- PHP Add commas to items with AND
- Replace the last comma with an & sign