• Hi,

    It sound weird, but is it possible to give a specified word a color?

    So if i add the word “Announcement” to the post title, it will be the color blue. And the other words in the post title will be black or something.

Viewing 15 replies - 1 through 15 (of 25 total)
  • Thread Starter Matthijs

    (@matthijs110)

    Anyone?

    Ayman

    (@aymanalzarrad)

    You can do this by adding a filter to the_title. Its not suggested to modify the core files of the theme instead create a child theme and apply your modifications there.

    1- Create a child theme and add this function to your child theme functions.php

    /* Function to add specific css class to a specific word in title */
    function word_color($title) {
    /* "Announcement" should be exact as the targeted word and its case
     * sensitive
     */
        $title = str_replace("Announcement","<h1 class=\"word_blue\">Announcement</h1>",$title);
        return $title;
    }
    add_filter('the_title', 'word_color');
    
    /* Now every time that the title contain the "Announcement" word
     * the word will be replaced with the second "Announcement" that have
     * the css class
     */

    2- Than in your child theme style.css file add somthing like this:

    .word_blue{
       color:blue;
    }

    Thread Starter Matthijs

    (@matthijs110)

    And if i want to do that with more words?

    You can also add that CSS class to a word by using span tags in the HTML –

    <span class="word_blue">This will be blue</span>
    Thread Starter Matthijs

    (@matthijs110)

    You mean i have to add it every time by hand in the post title settings? Cause i won’t want that.

    I just want to have it automatically

    Ayman

    (@aymanalzarrad)

    Just repeat the str_replace(); function. For Ex:

    /* Function to add specific css class to a specific word in title */
    function word_color($title) {
    /* "Announcement" should be exact as the targeted word and its case
     * sensitive
     */
        // THE FIRST WORD
        $title = str_replace("Announcement","<h1 class=\"word_blue\">Announcement</h1>",$title);
    
        // THE SECOND WORD
        $title = str_replace("BOO","<h1 class=\"word_red\">BOO</h1>",$title);
    
        return $title;
    }
    add_filter('the_title', 'word_color');
    
    /* Now every time that the title contain the "Announcement" word
     * the word will be replaced with the second "Announcement" that have
     * the css class
     */
    Thread Starter Matthijs

    (@matthijs110)

    In /includes/functions.php

    Or the normal one?

    Ayman

    (@aymanalzarrad)

    The functions.php file that is in the root folder of your child theme:
    ../wp-content/themes/child-theme/functions.php

    Thread Starter Matthijs

    (@matthijs110)

    It doesnt work ??

    Ayman

    (@aymanalzarrad)

    The code I gave you is working. did you paste the code in the path I told you? did you give attention to the comments in the code to customize it for your needs?

    Thread Starter Matthijs

    (@matthijs110)

    attention to the comments in the code to customize it for your needs

    What?

    Ayman

    (@aymanalzarrad)

    This:

    /* “Announcement” should be exact as the targeted word and its case
    * sensitive
    */

    Thread Starter Matthijs

    (@matthijs110)

    Can you give me a example of CSS? With the word in it?

    Ayman

    (@aymanalzarrad)

    Maybe I wasn’t clear enough so I will try to explain you how the code is working.

    $title = str_replace("Announcement","<h1 class=\"word_blue\">Announcement</h1>",$title);

    The First Announcement is the target. that we are replacing it with:

    <h1 class=\"word_blue\">Announcement</h1>

    So that now our Second Announcement has a css class which is:
    word_blue that we can use in our style.css like this:

    .word_blue{ color:blue; }

    I hope I was more clear this time

    Thread Starter Matthijs

    (@matthijs110)

    I did that exactly, it still default black ??

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Word specified colors’ is closed to new replies.