• Resolved ajcke

    (@ajcke)


    I’m using the following code to display the root page name in the header of my site. I also need the header to change per page root ID. This is the part I’m not sure how to do.

    To explain this site is for a school district. There are three main sections of the school – District, Elementary, JH & High School. Currently if a user navigates to District pages the word District displays in the header. The same goes for the other sections of the school.

    <?php $page_title = ( empty( $post->ancestors ) ) ? get_bloginfo('name')  : get_the_title(end( $post->ancestors )); ?>
    <h1><?php echo $page_title ; ?></h1>
Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz

    (@bcworkz)

    The post ‘ancestors’ property is not part of the base WP installation, it must be your theme or a plugin creating this? It appears this could be leveraged to do what you want, but not being familiar with it, it’s difficult to be specific. You might possibly be able to use a switch/case structure where, if a page has a particular ancestor, that case is identified and loads a particular header image accordingly.

    To do something similar with the base installation, the pages would need to be properly organized hierarchically. You then get the parent page ID from the ‘post_parent’ property and check that page’s parent in turn. Once you find a parent ID of 0, you have found the root page who’s ID could be fed into the switch/case structure for the specific code to load the associated image and whatever else might be required.

    Thread Starter ajcke

    (@ajcke)

    How would I code something like this?

    if page root id = 1 display header image1.png
    if page root id = 2 display header image2.png
    if page root id = 3 display header image3.png
    else display header imageDefault.png

    That part is easy. Something like (untested…):

    if ($root_id == 1) {
        echo "img1.jpg";
    }
    elseif ($root_id == 2) {
        echo "img2.jpg";
    }
    // Add in as many extra ID's as you need in their own elseif() blocks
    else {
        echo "default.jpg";
    }

    The only thing that I’m not sure of how to get the root ID. As bcworkz said, this isn’t part of the normal WP install, so this is just a very vague guess…

    $root = end($post_ancestors);
    $root_id = $root->ID
    Thread Starter ajcke

    (@ajcke)

    Okay. Now I’m getting somewhere. I tried the following.

    if ($page_title == "Elementary") {
        echo "img1.jpg";
    }
    elseif ($page_title == "High School") {
        echo "img2.jpg";
    }
    // Add in as many extra ID's as you need in their own elseif() blocks
    else {
        echo "default.jpg";
    }

    This puts the text img2.jpg on the page. I think the proper solution would be to change the css class or id since I’m using this to change the header background image. I tried the following but there is something wrong with the code. My page displays white.

    <?php
    if($page_title == "Elementary") {
        $div_id = 'topHeaderElementary';
    }
    else if ($page_title == "High School") {
        $div_id = 'topHeaderHighSchool';
    else {
        echo "topHeader";
    }
    ?>
    <div id="<?php echo $div_id ?>">div content</div>

    Moderator bcworkz

    (@bcworkz)

    Ah, the white page of death 🙂

    Change false to true in the following line that is found in your wp-config.php file:
    define('WP_DEBUG', false);

    Reload your page and you should get error messages telling you what the problem is. Do not leave this set to true for any length of time on a live site, it’s a bit of a security risk.

    Thread Starter ajcke

    (@ajcke)

    I finally have the chance to get back to this. The debug message says:

    Notice: load_plugin_textdomain was called with an argument that is deprecated since version 2.7 with no alternative available. in /wp-includes/functions.php on line 2714 Notice: load_plugin_textdomain was called with an argument that is deprecated since version 2.7 with no alternative available. in /wp-includes/functions.php on line 2714 Parse error: syntax error, unexpected T_ELSE in /wp-content/themes/themename/header.php on line 115

    Line 115 of header.php
    else {

    Here’s the code.

    <?php
    if($page_title == "Elementary") {
        $div_id = "topHeaderElementary";
    }
    elseif ($page_title == "High School") {
        $div_id = "topHeaderHighSchool";
    // Add in as many extra ID's as you need in their own elseif() blocks
    else {
        $div_id = "topHeader";
    }
    ?>
    <div id="<?php echo $div_id ?>">div content</div>

    I can see exactly where your problem is. You’re missing a closing } on the elseif() block. To fix that you need to add } on the line above else {.

    As for the first error mesage, I’m not sure on that one. The problem is that somewhere the call to the function isn’t happening corectly, but that’s going to be harder to track down.

    Thread Starter ajcke

    (@ajcke)

    Yep…closing bracket. The div id is now changing depending on the root id or root page. I’ll try to get it completely setup next week. Very cool. Thanks for all your help!

    Thread Starter ajcke

    (@ajcke)

    Resolved

    Thread Starter ajcke

    (@ajcke)

    Thanks catacaustic and bcworkz!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Display Specific Header Image According to Page Root ID (Ancestor)’ is closed to new replies.