• <div id="header" class="services"></div>

    I have a services page with child pages in the services section and a product page with child pages in the product section etc. I want to display a different header image using css depending what section of the site the page is located in. So in the code above I want to be able to name the class ‘services’ if the page is ‘services’ or a child of ‘services’ and rename it ‘products’ if the page is ‘products’ or a child of ‘products’.

    Is it possible to do that?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Take a look through this for ideas:

    conditional tags

    Thread Starter subfusioncom

    (@subfusioncom)

    Thanks for that. I can now make the class the same as the page name but how do I make the class the name of the parent page for a sub-page?

    Thread Starter subfusioncom

    (@subfusioncom)

    if (is_page('widgets')) {
                 echo 'home';
            } elseif (is_page('about')) {
                 echo 'about';
    		} elseif (is_page('services')) {
                 echo 'services';
    		} elseif (is_page('products')) {
                 echo 'products';
    		} elseif (is_page('contact')) {
                 echo 'contact';
    		} elseif ($post->post_parent) {
    			 the_title('','',false) echo '[post_parent]';
    		}

    I can create the right class for a specific page as in the code above but in the last line I’m trying to print the parent page name so that the sub page inherits the parent page class. I realise [post parent] isn’t going to work! I’m still new but once I’ve grasped this then I think I can be more self sufficient.

    Thanks for your help so far adambrown any more suggestions would be appreciated….

    Am I nearly there yet?

    Thread Starter subfusioncom

    (@subfusioncom)

    I’m getting there….

    The code below will write a sub page class as the parent page ID number but I’d like it to write the class as the parent page name (numbers dont work in css).

    if (is_page('naturopathy')) {
                 echo 'home';
            } elseif (is_page('about')) {
                 echo 'about';
    		} elseif (is_page('services')) {
                 echo 'services';
    		} elseif (is_page('products')) {
                 echo 'products';
    		} elseif (is_page('contact')) {
                 echo 'contact';
    		} elseif ($post->post_parent) {
    			 echo $post->post_parent;
    		}

    Numbers do work in CSS. Example: <div class="page_5"> for page ID 5.

    So you could solve this like so:

    <?php
    if (is_page() && !$post->post_parent)
      the_ID();
    elseif(is_page())
      echo $post->parent;
    ?>

    That will print the number, and just combine it with a slug as shown in my html example.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘parent child question’ is closed to new replies.