WordPress.org

Ready to get started?Download WordPress

Forums

Trying to set different backgrounds (say) for different sets of Pages (5 posts)

  1. stvbaker
    Member
    Posted 4 years ago #

    I have a site with a blog and with 4 sections that contain Pages. In the Dashboard I have used the Page Order number column to give each of the four Page sections a range of numbers different from the other three sections. Say, the "About" section is from 1000 to 1999, and all pages in this section are multi-level descendants of the About page. Then I set the "Contact" section to Page Order numbers from 2000 to 2999 and make all pages in that section into multi-level descendants of the Contact page.

    For example, something simple. Say I want to give all pages in the About section a green background, while giving all the pages in the Contact section a yellow background, to help people know what section they are in.

    I can think of two ways I could do that. However, I haven't been able to figure out how to do either one.

    Option 1: Write a PHP function that uses an IF statement, like "if (is_page() && IF(Page Order Number is greater than 999 and less than 2000)), then set background to green. My problem is that I can't find a variable that I can use to get the Page Order Number for the current page so that I can use it in the IF statement. The only Page Order thing I have found is sort_column=menu_order, which is not what I need. Does anyone know how to reference the Page Order Numbers set in the Dashboard in a PHP IF statement? Is there a specific get_ or variable that will obtain the current_page's Page Order Number?

    Option 2: Write a PHP function that uses an IF statement to check the page to see if it is a descendant of the About page or the Contact page. I've only been able to find a parent/child link which only goes up one level, not to the Parent page of all the pages in that section. If I am on a page that is 5 levels down from the parent About page, how do I test for the About page or the Contact page as the ultimate parent page for the current_page? If(is_page() && current_page has ultimate parent page of About), then make background green. Is there an ancestor variable that will obtain the About page as ultimate parent?

    Of course, maybe there is an Option 3 that is better than either of these.

    I am pretty new to WordPress. Any help you could give me in resolving this will be very greatly appreciated. Thank you!

    Steve

  2. syncbox
    Member
    Posted 4 years ago #

    http://codex.wordpress.org/Conditional_Tags

    Read that, especially the part about is_tree() in snippet#3

    Add that to the functions.php file in your theme, per directions or to the includes functions.php (though you may end up having to back this file up or redo the addition after upgrades)

    is_tree('ID#') (id of the parent page, which you can get from the list of pages in your admin by hovering over a page listed and looking in the status bar for the post number (pages are posts, too)

    Use conditional logic with the is_tree function to echo a style for the parent and immediate sub-pages (note, this doesn't apply to sub-subs which can be a GOOD thing)

    See also:
    http://wordpress.org/support/topic/320406?replies=8#post-1257337

    hth

    but of course, this doesn't go down 5 levels.... still, you could add the levels in the is_tree logic

  3. syncbox
    Member
    Posted 4 years ago #

    BTW, just want to say that from your first post, it sounds like that was a huge chore! yikes!

    Maybe those should be categories so you can use the post_is_in_descendant_category function

    http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category

  4. stvbaker
    Member
    Posted 4 years ago #

    Syncbox, thank you very much for your posts. Lots of information to check. Unfortunately, this doesn't help. It appears to be a huge chore to you, but I know how to use a few If statements to do it easily. If I have 6 sections on my website, all I need are 6 If statements. That's easy, not a huge chore. The problem is the variables or functions I need to use to do this. I can't figure out what they are called or even if they exist.

    I cannot use the ID# in a simple manner. ID#s are assigned arbitrarily to each post/page as I first create it. So trying to determine whether a page is in section A or section B requires me to list every single ID# one by one in the If statement for that section. Same problem with post_name or post_title. However, the Page Order Number that I set in my Dashboard would be perfect. Unfortunately, I can't find any information anywhere that says how to access the Page Order Number in a PHP function when WordPress creates the page. If I knew that variable name, I wouldn't need to use "ID#,ID#,ID#...". I could use If("$page_order_number" >1999 && "$page_order_number" <3000) then... Simple. But I can't find anywhere that talks of a $page_order_number variable. Do you know if this exists somewhere, and if so, what it is actually called?

    Likewise, I cannot use the post_parent variable to screen, because that only goes up one level. Determining the subpage parent of a subsubpage cannot tell me if the subsubpage is in section A or section B. For that, I need to be able to use the is_tree() function and replace the post_parent variable with a variable named something like "ultimate_post_ancestor". Do you know if such a variable exists, and if so, what it is called?

    Finally, I cannot use is_category() because Pages are not categorized. The only variables I know for Pages are the page's ID#, page title, and page name. What I need is either one of (1) $page_order_number or (2) $ultimate_post_ancestor or their equivalents.

    Do you have any suggestions? I will be very appreciative of any ideas posted here, and I thank you for your time.

    Steve

  5. stvbaker
    Member
    Posted 4 years ago #

    SORRY!! Please ignore the above post. It got posted without my thinking it was posted. Again, I'm sorry. This is the post I wanted to have appear in the thread.

    Syncbox, your information put me on the right track. I finally figured out how to write a function to get the number I need. Here is the code I came up with. Note that I use "seb" to mark any function or variable that is mine. Also, a blog page, or an archive or category page, etc. will return the value of zero, because they fail the Pages test.

    function seb_get_menu_order_num( $seb_post_id) {
    $seb_menu_order_num = 0;
    $seb_post_id = (int) $seb_post_id;
    if ( false == is_page($seb_post_id))
    	return $seb_menu_order_num;
    
    $post = get_post( $seb_post_id );
    $field = 'menu_order';
    $seb_menu_order_num = (int) $post->$field;
    return $seb_menu_order_num;
    }

    I'm thinking of making $seb_menu_order_num a global variable. If I can do that, then I can call this function before the header, to set the number into the variable, and then I can use the variable in other locations down the post without having to call the function again (and again).

    Please let me know if you (1) see any way to make this code leaner, or (2) have a different way to do this that is more efficient.

    Thanks!

    Steve

Topic Closed

This topic has been closed to new replies.

About this Topic