bboybeatle
bboybeatle

Reputation: 567

Best way to omit elements from Wordpress header/footer on certain pages

Most of my pages on my Wordpress site have the same header, and footer. The header includes a form and menu and contact buttons. The footer also contains a form.

I have a number of pages with completely custom layout. eg. page-personal-training-landing.php & page-beach-body-ready.php. For these pages I am omitting elements from the header and footer.

Currently I am using an 'if not' statement to display those elements on all pages except those custom ones...

<?php if ((!is_page( 'contact' ))&&(!is_page('beach-body-landing'))&&(!is_page('4-week-body-transformation'))&&(!is_page('beach-body-ready'))&&(!is_page('personal-training-landing'))){?>
<div class="contact-button"></div>
<?php } ?>  

...but as you can see this code is getting very long, and I'll constantly be adding new pages to it when they are created.

I thought templates would be the solution.. if(!is_page_template( 'noheadernofooter-page.php' )... but unfortunately Wordpress defaults to that template layout, before it defaults to the custom page layout...

Hierarchy of Wordpress' page template choice...

  1. custom template file – The page template assigned to the page. See get_page_templates().
  2. page-{slug}.php – If the page slug is recent-news, WordPress will look to use page-recent-news.php.
  3. page-{id}.php – If the page ID is 6, WordPress will look to use page-6.php.
  4. page.php
  5. singular.php
  6. index.php

I'm thinking now, maybe I should use Advanced custom fields to add some check boxes. eg. Hide header buttons (Y/N), and then query the value of those on the current page.

Does that sound like the most logical solution?

Does anyone know how this is commonly done without other plugins?

Thankyou

Upvotes: 1

Views: 1199

Answers (3)

Peter HvD
Peter HvD

Reputation: 1661

Here's another way, utilising get_header() as already mentioned:

Get the post slug and pass that into get_header. That way your pages will always look for a custom header file, but if one is not found WP will default to the standard header.php:

if ( is_page() ) {
    global $post;
    get_header( $post->post_name );
}

It does mean you will need custom header.php files for each page, so you could end up with hundreds of header files, but you can circumvent that somewhat by using symlinks.

Upvotes: 0

Dan
Dan

Reputation: 437

In my opinion, the way you are doing it is already the right way. I might add includes to make it easier to maintain, but if statements are the way to go in my opinion. Take a look at some code I have used (modified for OP) as an example:

if( (is_page( 'contact' ) ) {

include( 'header-contact.php' ); // include this file on contact page

} elseif( is_page('beach-body-landing') ) {

include( 'header-body-landing.php' ); // include that file on body landing page

} else {

get_header(); // otherwise use our normal header

}

It is unfortunate there is not a hook to hook into get_header(), get_footer() or get_sidebar(), because then we could do it all in one place. This is just the nature of wordpress development.

EDIT

To include what you mentioned below: you could tweak this like:

if( (is_page( 'contact' ) ) {

get_header('contact'); // use `header-contact.php`

} elseif( is_page('beach-body-landing') ) {

get_header('body-landing'); // use `header-body-landing.php`

} else {

get_header(); // otherwise use our normal header

}

Hope this helps!

Upvotes: 0

bboybeatle
bboybeatle

Reputation: 567

I THINK I've found the most elegant solution here...

https://stackoverflow.com/a/36521754/3656408

"You can create new header and footer files, say "header-mytemplate.php" and "footer-mytemplate.php". Create what ever structure you want into it. And in your new tempalte call these header and footer, like this get_header('mytemplate'); and get_footer(mytemplate);."

If anyone thinks there's a better solution I'd still like to know

Upvotes: 2

Related Questions