WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Assigning a Different Colour to Each Page (3 posts)

  1. Wardrop
    Member
    Posted 5 years ago #

    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!

    Thanks

  2. esmi
    Forum Moderator
    Posted 5 years ago #

  3. Mark / t31os
    Moderator
    Posted 5 years ago #

    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: http://codex.wordpress.org/Function_Reference/get_post_meta
    $myfield1 = get_post_meta($post->ID,'field_1_name',true);
    $myfield2 = get_post_meta($post->ID,'field_2_name',true);
    
    // Ref: http://codex.wordpress.org/Conditional_Tags
    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)..

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.