• Hi,

    Can someone please help me? I know very little about PHP.

    I would like to have a different header on one or my pages and I found this piece of code to insert in my header file that seems like it could work but where should I put it in the Header file?

    Thanks!

    <?php if (is_page()) echo 'ALTERNATE HEADER';
    else echo 'MAIN HEADER';?>
    <?php
    /**
     * The Header for our theme.
     *
     * Displays all of the <head> section and everything up till <div id="main">
     *
     * @package WordPress
     * @subpackage Twenty_Eleven
     * @since Twenty Eleven 1.0
     */
    ?><!DOCTYPE html>
    <!--[if IE 6]>
    <html id="ie6" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if IE 7]>
    <html id="ie7" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if IE 8]>
    <html id="ie8" <?php language_attributes(); ?>>
    <![endif]-->
    <!--[if !(IE 6) | !(IE 7) | !(IE 8)  ]><!-->
    <html <?php language_attributes(); ?>>
        <!--<![endif]-->
        <head>
    	<link rel="shortcut icon" href="https://bearleaderchronicle.com/site/wp-content/themes/blc/images/favicon.ico" type="image/x-icon" />
            <meta charset="<?php bloginfo('charset'); ?>" />
            <meta name="viewport" content="width=device-width" />
            <title><?php wp_title(); ?></title>
            <link rel="profile" href="https://gmpg.org/xfn/11" />
            <meta name="viewport" content="user-scalable = yes">
            <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo('stylesheet_url'); ?>" />
            <!--[if lt IE 9]>
            <script src="<?php echo get_template_directory_uri(); ?>/js/html5.js" type="text/javascript"></script>
            <![endif]-->
            <?php
            /* We add some JavaScript to pages with the comment form
             * to support sites with threaded comments (when in use).
             */
            if (is_singular() && get_option('thread_comments'))
                wp_enqueue_script('comment-reply');
            /* Always have wp_head() just before the closing </head>
             * tag of your theme, or you will break many plugins, which
             * generally use this hook to add elements to <head> such
             * as styles, scripts, and meta tags.
             */
            wp_head();
            ?>
            <style type="text/css">
                #outerImageContainer,
                #imageDataContainer,
                #overlay{
                    background-color: <?php if (get_field("background_color")) the_field("background_color") ; ?> !important;
                }
            </style>
            <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/css/custom.css" />
            <link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/css/media.css" />
            <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
        </head>
    
        <body <?php body_class(); ?> style="background-color: <?php if (get_field("background_color")) the_field("background_color"); ?>">
            <div id="page" class="hfeed">
                <header id="branding" role="banner">
                    <div id="header-content-area">
    
                        <?php
                        // Check to see if the header image has been removed
                        $header_image = get_header_image();
                        if ($header_image) {
                            // Compatibility with versions of WordPress prior to 3.4.
                            if (function_exists('get_custom_header')) {
                                // We need to figure out what the minimum width should be for our featured image.
                                // This result would be the suggested width if the theme were to implement flexible widths.
                                $header_image_width = get_theme_support('custom-header', 'width');
                            } else {
                                $header_image_width = HEADER_IMAGE_WIDTH;
                            }
                            ?>
    
                            <a href="<?php echo esc_url(home_url('/')); ?>">
                                <?php
    //                            if (is_singular() && has_post_thumbnail($post->ID) &&
    //                                    ( /* $src, $width, $height */ $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array($header_image_width, $header_image_width)) ) &&
    //                                    $image[1] >= $header_image_width) :
    //                                // Houston, we have a new header image!
    //                                echo get_the_post_thumbnail($post->ID, 'post-thumbnail');
    //                            else :
                                    // Compatibility with versions of WordPress prior to 3.4.
                                    if (function_exists('get_custom_header')) {
                                        $header_image_width = get_custom_header()->width;
                                        $header_image_height = get_custom_header()->height;
                                    } else {
                                        $header_image_width = HEADER_IMAGE_WIDTH;
                                        $header_image_height = HEADER_IMAGE_HEIGHT;
                                    }
                                    ?>
                                    <img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
                                <?php
    //                            endif;
                                ?>
                            </a>
                        <?php } ?>
                    </div>
                    <div id="header-side-area">
                        <div id="logo">
                            <a href="<?php echo home_url('/'); ?>">
                                <img src="<?php echo get_stylesheet_directory_uri() . '/uploads/logo.png'; ?>" alt="<?php bloginfo('name'); ?>" title="<?php bloginfo('name'); ?> Website"/>
                            </a>
                        </div>
                        <hgroup>
                            <h1 id="site-title"><span><a href="<?php echo esc_url(home_url('/')); ?>" title="<?php echo esc_attr(get_bloginfo('name', 'display')); ?>" rel="home"><?php bloginfo('name'); ?></a></span></h1>
                            <h2 id="site-description"><?php bloginfo('description'); ?></h2>
                        </hgroup>
                    </div>
                    <div class="clear"></div>
    
                    <div id="header-navigation-area">
    
                        <?php if (has_nav_menu('primary')) { ?>
                            <nav id="access" role="navigation">
                                <?php
                                wp_nav_menu(
                                        array(
                                            'container_class' => 'primary-menu',
                                            'theme_location' => 'primary',
                                            'link_before' => '<span>',
                                            'link_after' => '</span>',
                                            'after' => '/',
                                            'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s<li class="clear"></li></ul>',
                                        )
                                );
                                ?>
                            </nav><!-- #access MN replaced sidebar menu with Serch box -->
                        <?php } ?>
                        <?php if (has_nav_menu('sidebar')) { ?>
                            <nav id="side-access" role="navigation">
                                <?php get_search_form(); ?>
                            </nav><!-- #access -->
    
                        <?php } ?>
                        <div class="clear"></div>
                    </div>
    
                </header><!-- #branding -->
    
                <div id="main">
Viewing 7 replies - 1 through 7 (of 7 total)
  • I don’t think that code would work.
    It’s specific to twenty eleven theme.
    If you want a different header on just one page, it’s probably better to do it via CSS, but until we see your site and see what them you are using it will be hard to say.

    Can you share a link please?

    Thread Starter mcnaden

    (@mcnaden)

    Thanks for the reply.

    The website is https://www.bearleaderchronicle.com and the page we want to have a different rotating header on is https://www.bearleaderchronicle.com/lodgings.

    Actually this is the twenty eleven theme, although heavily customized.

    The way to have a custom header on one page would be something like this:

    <?php if ( is_singular() && $post->ID == 123 ): ?> // use the page ID that you want in here
        <?php get_header( 'special' ); ?>
    <?php else: ?>
        <?php get_header(  ); ?>
    <?php endif; ?>

    The big part is that you’ll need to use two different header files – your standard one as header.php and the once-off one at header-special.php.

    If you only need a small part of the header changed, and not the entire lot, you can place the code above in your header.php file and change the calls to get_header() to whatever different code you want to use.

    Thread Starter mcnaden

    (@mcnaden)

    Thanks catacaustic for the suggestion. I have two header images set that randomly load on each refresh. I don’t need a completely different header on our new page, just two different images for the new page. I seem like an if statement similar to your suggestion would be possible. Could you be more specific about where this code would be inserted in my header file above?

    Thanks

    You’d replace the existing image and place that code in that area. It amy take a bit of playing around to get it right in your theme so see where it fits best.

    Thread Starter mcnaden

    (@mcnaden)

    Thanks catacaustic. I am still stuck because with my limited knowledge of php I can’t identify where in the header the images are being called. Can you point out where that occurs? If I know that I can experiment with the code you suggested.

    Thanks

    As a very vague guess… Maybe replacing this?

    <img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom header on ONE page’ is closed to new replies.