WordPress 文章标题显示各种颜色(非主流效果)

如何让WordPress文章标题自动显示各种颜色效果呢?这其实有点非主流效果,但一些个人博客就喜欢这效果,下面教大家如何实现:

「后台」→「外观」→「编辑」→ 「functions.php」文件,把下面的代码添加进去:

function hacklog_random_title_color($title, $id)
{
$id = (int) $id;
static $colors = array(
‘rgb(153,153,0);’,
‘rgb(68,153,102);’,
‘rgb(85,119,221);’,
‘rgb(187,102,34);’,
‘rgb(153,68,170);’,
‘rgb(119,136,255);’,
‘rgb(0, 104, 28);’,
‘rgb(121, 6, 25);’,
‘rgb(91, 16, 148);’,
‘rgb(200, 137, 0);’,
‘rgb(204, 0, 96);’,
‘rgb(0, 148, 134);’,
‘rgb(185, 0, 56);’,
‘rgb(132, 102, 0);’,
‘rgb(51, 0, 153);’,
‘rgb(0, 131, 145);’,
);
$index = $id % 16;
$style_color = ‘style=”color: ‘ . $colors[$index] . ‘” ‘;
return ‘<span ‘ . $style_color . ‘>’ . $title . ‘</span>’;
}
{
add_filter(‘the_title’, ‘hacklog_random_title_color’, 99, 2);
//&lt;span style=&quot;color: rgb(121, 6, 25);&quot; &gt;最近文章title属性去除HTML标签&lt;/span&gt;
function strip_all_attribute_tags($safe_text, $text)
{
$safe_text = str_replace(
array(‘&lt;span style=&quot;’, ‘&quot; &gt;’, ‘&lt;/span&gt;’), array(‘<span style=”‘, ‘” >’, ‘</span>’), $safe_text);
return strip_tags($safe_text);
}
add_filter(‘attribute_escape’, ‘strip_all_attribute_tags’, 99, 2);
}

# 更多WordPress技巧,请关注「WordPress专题