Support » Themes and Templates » Assigning a Different Colour to Each Page

  • Resolved Wardrop


    I would like to have each page (not post) of my wordpress blog to have a different colour associated with it. To define the colour to use for a specific page, I’ll be assigning a custom field to each page in the form of…

    Field Name: colour
    Field Value: CSS friendly colour value (eg. #FFFFFF, white, rgb(255,255,255)).

    The problem I’m having, is trying to find out how to list the pages and extract the value of this custom field for each page. Note, the colour value of a page also affects how it’s displayed in the list of pages.

    I have plenty of experience with PHP, MySQL and other such technologies, but I’m new to wordpress so would like someone to suggest the best possible way to do this in wordpress (in terms of preserving compatibility with future versions).

    What would be ideal in my mind, is simply a PHP array containing the necessary information such as title, page id and of course the keys and values of all custom fields associated with each page.

    Any help is very much appreciated!


Viewing 2 replies - 1 through 2 (of 2 total)
  • esmi


    Forum Moderator

    If you know PHP, then you’re only missing an understanding of the built-in WP functions.

    To give you a visual idea, it’s as simple as…

    // Ref:
    $myfield1 = get_post_meta($post->ID,'field_1_name',true);
    $myfield2 = get_post_meta($post->ID,'field_2_name',true);
    // Ref:
    if(is_page()) {
      if($myfield1) {
        // If first field has a value do what's here.
      else {
        // First field not present
      if($myfield2) {
        // If second field has a value do what's here.
      else {
        // Second field not present

    Hope that helps..

    Of course you might want to check the field values to ensure you are getting valid RGB or HEX values…

    You’d likely just need a preg_match if/else setup… (or any number of ways)..

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Assigning a Different Colour to Each Page’ is closed to new replies.