WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Page Specific Header Images (8 posts)

  1. macart
    Member
    Posted 4 years ago #

    Im trying to load a different header for different pages.
    My header is actually placed lower down the page in my index.php file, not sure if this makes a difference code wise.

    <?php get_header(); ?>
    <div id="mainContent">
    <div class="contentBanner"> <?php if (is_page('home') ) {
    $hdrimg = 'home.jpg';} elseif ( is_page('store') ) {
    $hdrimg = 'store.jpg';} elseif ( is_page('about') ) {
    $hdrimg = 'about.jpg';} elseif ( is_page('gallery') ) {
    $hdrimg = 'gallery.jpg';} elseif ( is_page('hidden') ) {
    $hdrimg = 'hidden.jpg';} ?></div>

    Its not working and I know Im doing something wrong, just not
    sure how to fix it.

    Thanks

  2. alism
    Member
    Posted 4 years ago #

    You don't appear to be doing anything with $hdrimg.

    Have a read of this, looks like it explains pretty much exactly what you want to do.
    http://wordpress.org/support/topic/276303

  3. macart
    Member
    Posted 4 years ago #

    "You don't appear to be doing anything with $hdrimg"

    Im new to this, not sure exactly what that means?

    Thanks for the link, scanning it for clues.

  4. syncbox
    Member
    Posted 4 years ago #

    I'd suggest using the post ID rather than the name/slug for conditional logic... who knows? You might change the name or slug.

    That said, if you are using Pages and sub-pages, consider using logic with is_tree(#) where the number is the post id of the parent Page... you'll have to read this:

    http://codex.wordpress.org/Conditional_Tags

    (snippet #4)

    and add the function to your functions.php file or to the wp-includes/functions.php file (which would make it custom and you'd have to track that in upgrades)

    function is_tree($pid) {      // $pid = The ID of the page we're looking for pages underneath
    	global $post;         // load details about this page
    	if(is_page()&&($post->post_parent==$pid||is_page($pid)))
                   return true;   // we're at the page or at a sub page
    	else
                   return false;  // we're elsewhere
    };

    example:
    you can put this directly into the image tag for the header image... eg:

    <img src="<?php bloginfo('template_directory'); ?>/whateverfolderyouareusing/<?php if (is_tree('1')) {echo 'frog';} elseif (is_tree('2')) {echo dog'; } else {echo 'default';} ?>.jpg />

    if that makes sense to you... where 1 and 2 are the appropriate id numbers for the Pages in question and of course, you can have as many elseifs as you need...

    You can use the same logic with (is_page(#)) and even build arrays if (is_page(#) || is_page(#) || is_page(#)) {

    or go from is_tree to is_page to is_category to is_front_page and so on.

    The beauty of is_tree() is that it only affects the direct child-pages of the parent, not deeper, which is very cool, imo.

    Hope that helps. I don't know about $hdrimg either, but will look into it to expand my skill set.

  5. iridiax
    Member
    Posted 4 years ago #

    You don't appear to be doing anything with $hdrimg

    Im new to this, not sure exactly what that means?

    You need to php echo the variable $hdrimg as part of the src in an image tag.

  6. macart
    Member
    Posted 4 years ago #

    <div class="contentBanner"><?php if(is_page('home')){
    echo '<img src="www.mysite.etc./banners/home.jpg" />';} ?></div>

    Tried echo, not working. What am I doing wrong?

  7. macart
    Member
    Posted 4 years ago #

    Im wondering if this isn't working because my header doesn't fall into the typical header.php file. My header is actually in my index.php file. Anyone know if this is an issue or how to get this workin. Thanks

  8. syncbox
    Member
    Posted 4 years ago #

    www. isn't a complete URL... it would have to be at least http://www.mysite.etc

    And if this is the FRONT page (setting a static page to be home), use is_front_page() rather than home... because it isn't home if you set it to be front page.

    <div class="contentBanner"><?php if(is_front_page()){
    echo '<img src="'.bloginfo("template_directory").'/imagefolder/nameofimage.jpg" />';} else {} ?></div>

    using elseif and else

    But this means you have to hardcode the image to be used for each page -- as opposed to usigng is_tree() to echo the image src, which allows for all the sub pages of said parent to share the same header image.

    HTH

Topic Closed

This topic has been closed to new replies.

About this Topic