Forums

[resolved] Conditional tags in header.php code for custom header on pages (8 posts)

  1. WMS Group
    Member
    Posted 2 years ago #

    Hi,

    I used the method described here to get a different header image depending on what page a visitor is on.

    My page is here.

    I think it is working, but there are broken image icons on top of the default header images. I am not sure if my image path is wrong (I'm sure that it is correct though) or if I have missed something in integrating it into the page.

    Below is the code for header.php:

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    ?>
    <?php
    if(is_page('2')){
    echo '<images/header-about.jpg" />';
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
    
    <head profile="http://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    
    <title><?php wp_title('&laquo;', true, 'right'); ?> <?php bloginfo('name'); ?></title>
    
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
    
    <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>
    
    <?php wp_head(); ?>
    </head>
    <body <?php body_class(); ?>>
    <div id="wrapper">
      <div id="small_slide"><img src="images/small_slide.png" width="431" height="249" /></div>
      <div id="header"><img src="images/header.jpg" width="899" height="283" /></div>

    Can anyone see something noticeably wrong?

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    <div id="header"><img src="images/header.jpg" width="899" height="283" /></div>

    try

    <div id="header"> <img src="<?php bloginfo('template_url'); ?>/images/header.jpg" width="899" height="283" /></div>

  3. WMS Group
    Member
    Posted 2 years ago #

    Okay, that worked... I did the same for the small_slide.jpg image and that was the trick. NOW, my page is actually supposed to bring in header-about.jpg as referenced in

    ?>
    <?php
    if(is_page('2')){
    echo '<images/header-about.jpg" />';
    }
    ?>

    but that doesn't seem to be working.

  4. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    should that bit of code be up in the <head></head> area of your template where it is now?

    Or down somewhere after the <body> opening?

  5. Jeremy Clark
    Moderator
    Posted 2 years ago #

    The problem is this code.

    <?php
    if(is_page('2')){
    echo '<images/header-about.jpg" />';
    }
    ?>

    All this is going to do is at the top of the header.php in the source code is put <images/header-about.jpg" />. I think I see what your trying to do. Try out this code.

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
    
    <head profile="http://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    
    <title><?php wp_title('&laquo;', true, 'right'); ?> <?php bloginfo('name'); ?></title>
    
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
    
    <?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>
    
    <?php wp_head(); ?>
    </head>
    <body <?php body_class(); ?>>
    <div id="wrapper">
      <div id="small_slide"><img src="<?php bloginfo('template_url'); ?>/images/small_slide.png" width="431" height="249" /></div>
      <div id="header">
    <img src="<?php bloginfo('template_url');
    if(is_page('2')) {
    echo '/images/header-about.jpg'; }
    else { echo '/images/header.jpg'; } ?>" width="899" height="283" /></div>
  6. WMS Group
    Member
    Posted 2 years ago #

    jermyclark13, thank you, that fixed it! Now was RVoodoo right in saying that it was placed incorrectly? Looks like you moved it down inside the "header" div.

  7. Jeremy Clark
    Moderator
    Posted 2 years ago #

    Yes like I said all the code was doing was simpling echoing out the code where it's placed. I just edited the code a little to have an else statement that when the if statement was true it would fall back to the default.

  8. WMS Group
    Member
    Posted 2 years ago #

    Great, I get it now. Thank you for your help :)

Topic Closed

This topic has been closed to new replies.

About this Topic