Changing specific word in Site Title WordPress

2019-09-21 09:36发布

问题:

how do I change the color of a specific word in the site title in Wordpress. I have been trying to figure this out for a whole day already. Please helpp!!!

回答1:

If the website is a website that you made in made in wordpress.com, there is no way to do so. If you are using wordpress.org software, that is wordpress on a custom hosted website, then you can do this by finding the title from the template(it will be a php function call or variable) and then replace it with the title as plain text and apply whatever CSS you need.

(Note that the title will not change if you update it from within wordpress CMS after you do this. Also changing template will undo this )



回答2:

add_filter( 'bloginfo', function( $output, $show ) {
    static $count = 0;
    if ( $show === 'name' && $count++ === 1 ) {
        $output = 'your <span style="color:red;">new</span> site title';
    }
    return $output;
}, 10, 2 );

UPDATE:

I tested this and it does work on theme Twenty Seventeen but does not work on another theme. As the site header is done by the theme a theme can really do anything it wants. This will work if the theme is using the conventional WordPress bloginfo() function to do the site title.

UPDATE2:

Unfortunately, bloginfo() is also used to generate the HTML title element where you do not want embedded HTML tags. In theme 2017 I can avoid this by only overriding bloginfo() on its second call to 'name'.

This is a very ugly solution and I would recommend either using a child theme or dynamically rewriting the title using JavaScript.