• Hi there,

    I want to do the following,

    if a user clicks on a page that is a sub-page of a top level parent page, then page.php should call a certain header. If the parent page is different then a different header is called.
    There are only two top level pages that I want this to happen on, so using the code from the codex page I tried this,

    <?php
    
    if 	(is_page() || $post->post_parent=="65") {
    	$subpagefix="/header-resources.php";
    } elseif (is_page() || $post->post_parent=="221") {
    	$subpagefix="/header-quickref.php";
    } else {
    	$subpagefix="/header.php" ; // Fall-through
    }	
    
    ?>
    <?php include (TEMPLATEPATH . $subpagefix ); ?>

    So this seems to work for the page 65, but subpages of page 221 call the header-resources.php.

    What have i done wrong?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter alst

    (@alst)

    Fixed it,

    the key was removing the “” around the page number.

    This code works now,

    <?php
    
    if 	(is_page() || $post->post_parent==65) {
    	$subpagefix="/header-resources.php";
    } elseif (is_page() || $post->post_parent==221) {
    	$subpagefix="/header-quickref.php";
    } else {
    	$subpagefix="/header.php" ; // Fall-through
    }	
    
    ?>
    <?php include (TEMPLATEPATH . $subpagefix ); ?>

    I use the ClassyBody Plugin to display different Headers or styles … You only have to insert this piece of code into your template (for the body-tag)

    <?php if(function_exists('classybody')) {?>
    <body class="<?php classybody(); ?>">
    <?php } else { ?>
    <body>
    <?php } ?>

    And in the page you want to display differently you add a custom field named classybody and give it a name.. e.g. “blue”

    Then you only have to edit you style.css like that…

    body.blue header{
    	color: #blue;
            other syles blah blah;
    }

    This allows you to create as many different styles as you like!!!
    Maybe this helps…
    cheers
    wearitwell

    Thread Starter alst

    (@alst)

    This is the definitive fix which allows sub-sub pages to also call the right header.

    Makes use of the fold_page_list plugin.

    <?php
    //this code uses fold_page_list plugin to generate array of ancestor pages and sets grandparent as top level page
    $g_page_id = $wp_query->get_queried_object_id();
    $ancestorIDs = _wswwpx_page_get_ancestor_ids($g_page_id);
    $grandParent = $ancestorIDs[1];
    
    ?>
    <?php
    //this code keeps lavalamp on the correct parent page when viewing a subpage, add elseif for other top level pages as appropriate
    if      ($grandParent==65) {
            $subpagefix="/header-resources.php";
    } elseif ($grandParent==221) {
            $subpagefix="/header-quickref.php";
    }
    else  $subpagefix="/header.php"
    
    ?>
    
    <?php  include (TEMPLATEPATH . $subpagefix);  ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sub page problem’ is closed to new replies.