Support » Fixing WordPress » Automatic text color

  • bucthree


    Im creating a WordPress theme from scratch for a client. The design of the home page can be seen here:

    The client wants to be able to change all the text and all the images through wordpress, and has no web development training. So I created custom fields for the text and images so he can type whatever he wants into the fields or replace an image on any of the fields. I want to be able to keep the yellow and white alternating text that I used for the headers, but am not sure on how to do that with a custom field. I know you can add in html to the custom field fields, but would there be some way to automate it for every other word to be colored yellow?


Viewing 1 replies (of 1 total)
  • Perhaps an easy way to do this would be to style anything inside <span> tags with the color yellow, then instruct your client on how to add those tags around text that should be highlighted.

    Alternatively, if you need to have every other word be yellow and want to automate the process, you could use a function like this in your functions.php file:

    function my_alt_color_text( $string ) {
        $array = explode( ' ', trim( $string ) );
        $open  = '<span>';
        $close = '</span> ';
        foreach( $array as $word ) {
            echo $open . $word . $close;
            $open  = ( $open == '<span>' ) ? '' : '<span>';
            $close = ( $close == '</span> ' ) ? ' ' : '</span> ';

    Then you can call that from your page template like this:

    <?php my_alt_color_text( $custom_field ); ?>

    EDIT: Note the space after the closing span tag, or in place of the span tag for every other word. You would then want to style the spans in your stylesheet.

Viewing 1 replies (of 1 total)
  • The topic ‘Automatic text color’ is closed to new replies.