2 differnt header.php files (best practice)

Permalink
So this has kinda been bugging me, lets say I am themeing 7 diffeent pages which all have the same exact type of header (header.php) then I have my home page which has the same type of header except there is a slider in the bod that would normally have words.

At this point should I just hand code the homepage header or is it possible to have 2 different header.php files? One for all the page types (header.php) and then one for just the homepage (homepageheader.php) ?

sambrody24
 
mhawke replied on at Permalink Best Answer Reply
mhawke
There are a couple of ways to achieve what you need to do.

If you look at the top of any of your template files (e.g. full.php or left_sidebar.php) you will see a line that says:

<?php $this->inc("elements/header.php");?>


This is what determines which header file is included with that page type. I have used the following technique in the past. Say, for example, that the page type file for your home page is 'left_sidebar.php' then replace the single line of code that includes header.php with the following:

<?php
$page = Page::getCurrentPage();
if ($page->getCollectionID()=1){
   $this->inc("elements/homepageheader.php")
} else {
   $this->inc("elements/header.php")
}
?>


This will include homepageheader.php only if it's the home page which is always page #1 (i.e. getCollectionID=1)

The other option that I have used if my home page design is somewhat different than the other pages would be to make a copy of the page type that most closely matches my desired design (left_sidebar.php for example) and call it home_page.php. In this new copy, change the single include line to read 'homepageheader.php' instead of 'header.php'.
If you take this route, you need to officially add the new 'Home Page' page type into your theme by going to Dashboard->Themes and 'Inspecting' your theme and 'Ok'ing the addition of the new 'Home Page' file. After this has been officially added to your theme, you can go to your home page and hover over 'Edit' and choose 'Design' and choose 'Home Page' as your page type.
The advantage of this way of doing it is that you are free to make structural changes to the home_page.php file to better reflect the unique design of your home page.
AlfonsoSanchez replied on at Permalink Reply
It looks like there is syntax error in you code, can you please look in to it and update the code.

<?php
$page = Page::getCurrentPage();
if ($page->getCollectionID()=1){
$this->inc("elements/homepageheader.php")
} else {
$this->inc("elements/header.php")
}
?>

Tanks
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
It looks like he just forgot to put the terminator sign (;).

Here is the code
<?php
$page = Page::getCurrentPage();
if ($page->getCollectionID() = 1){
$this->inc("elements/homepageheader.php");
} else {
$this->inc("elements/header.php");
}
?>


Rony
Ale replied on at Permalink Reply
And to avoid weird results you should use comparison == instead of assingment =