How do I get my pages to grab a different header with the PHP and such?
June 21, 2011 11:09 AM   Subscribe

How to get my pages to grab a different header with the PHP and such?

I am working on a site where I have created a different header for all the pages past the main index page, as I need to get more above the fold on those pages.

I have created a new header php file called header-sub.php and in it I have reassociated the styles and classes and such and reflected those changes in the style sheet.

However, I cant seem to figure out how to get these pages (also php docs) to grab the right header. They seem to be continuing to grab the old one.

Any idears?

The code in question originally goes:

<>

What should I change it to instead?
Thanks! Ive searched google for this already and I dont seem to be finding it (or I may just be out of my depth here, which seems more likely)
posted by Senor Cardgage to Computers & Internet (15 answers total)
 
Response by poster: Dammit. Cant seem to get it in here.
Tried pastebin and its not working
posted by Senor Cardgage at 11:15 AM on June 21, 2011


Try something like this:


if(strlen($_SERVER['PHP_SELF']) > 1){
include 'header-sub.php';
} else {
include 'header.php';
}


$_SERVER['PHP_SELF'] contains the path, without the domain name. Here's the complete documentation on $_SERVER.
posted by hobgadling at 11:25 AM on June 21, 2011


Response by poster: Can you make that dumber for me plz?
Sorry. I'm new.
posted by Senor Cardgage at 12:27 PM on June 21, 2011


You might see what hobgadling is getting at by putting this in your code, to see what gets returned:

echo $_SERVER['PHP_SELF'];

From what you're saying though, I think you could probably do this with css (with no need for the header-sub.php) and a body class specific for the page. In your header.php change:

<body>

to something like:

<?php

// break the path up into an array
$pathArray = explode("/", $_SERVER['PHP_SELF']);

// get the current page, which is the last item in the path array
$currentPage = $pathArray[count($pathArray) - 1];

// clean up the path by removing ".php", "/", and " "
$currentPage = str_replace(" ", "-", $currentPage);
$currentPage = str_replace("/", "-", $currentPage);
$currentPage = str_replace(".php", "", $currentPage);

// add the currentPage to the body class
?>
<body class="page-<?php echo $currentPage ; ?>">


--------
That will leave you with a body tag something like:

<body class='page-home'>

Then in your css, you can target things using the body class:

#header { /* regular styles for the subpage header */ }

.page-home #header { /* styles for the homepage's header */ }
posted by backwards guitar at 12:51 PM on June 21, 2011


I'm a little unsure how you have it set up and what you're trying to do since the code you tried to paste didn't work. It sounds like you want to include one file on one page and a different file on all the others.

If so, can you set a variable on the main page like $main = true;

and then do an if statement that is something like
if($main){
//include the main header file
}
else{
//include the subheader
}
posted by lurking_girl at 1:02 PM on June 21, 2011


Response by poster: Actually what Im doing may be simpler than all that.

I am building my portfolio site on Wordpress.

I have like 3 different kinds of pages..

Single Post
single.php

Page Template
page.php

Category
category.php

Main Index
index.php

They all start with this code
posted by Senor Cardgage at 1:18 PM on June 21, 2011


Does your body class have the page class already in it? Usually "page-6" or something like that. The home page will have a class of "home". More recent themes tend to have this built in.

Chances are it'd be easier to just use css and target the body class, since if it's just a visual change, you shouldn't need to alter the code. I use it all the time for a big banner on the home page, and a smaller banner on subpages. If it doesn't have the body class, you should be able to add it to your theme pretty easily. You'll need to edit the body tag in your header.php file. Here's how:

Wordpress Body Class Implementation
posted by backwards guitar at 1:27 PM on June 21, 2011


Response by poster: My body class doesnt appear to refer to any pages
posted by Senor Cardgage at 1:35 PM on June 21, 2011


Check out the Multi Headers section on this page:
http://codex.wordpress.org/Function_Reference/get_header

So, I think you'd want to do something like (assuming the page that's different is set to the homepage.
if ( is_home() ) :
get_header();
else :
get_header('sub');
endif;

Or, if it's only the main index.php that needs to be different you can just change the get_header on all those other pages to be get_header('sub')

I can't really test this right now, but it seems like that should work?
posted by lurking_girl at 1:42 PM on June 21, 2011


Odd, I thought Wordpress included it since 2.8. Do you have:

<body <?php body_class(); ?>>

in your header.php?

If not, you should be able to add it by opening up (or creating) functions.php in your theme folder and adding the following:
// add post id to the body class

function post_id_class($classes) {
	global $post;
	$classes[] = "page-" . $post->ID;
	return $classes;
}
add_filter('body_class', 'post_id_class');

posted by backwards guitar at 1:44 PM on June 21, 2011


Sorry, by "if not" I meant if you don't have the page class, but do have the body_class function in your body tag.

You'l want to update your body tag as above before the function I gave you will do anything. Sorry
posted by backwards guitar at 1:49 PM on June 21, 2011


I think lurking_girl has the right idea, given that you already have a separate header file created. Here's what I would do, because of the way that WP template files inherit, if you really only want the home page to be different.

Rename your current header.php to header-home.php and your current header-sub.php to header.php. That way your "sub" header style gets used for any default get_header() call. Then, in your index.php file, change
get_header() to
if(is_home()) { get_header('home'); }
else { get_header(); }


That way, you only have to make a change to one file, and the "sub" styles will get used on any other page that defaults to index.php in the absence of a specific template file (like Archives, for example, if you don't have an archives.php in your theme).
posted by ashirys at 2:07 PM on June 21, 2011


You could also try the wordpress 'dynamic headers' plugin which give you different headers on each page.
posted by the fish at 2:51 PM on June 21, 2011


<body class="page-<?php echo $currentPage ; ?>">

Don't do this. You've just introduced a XSS vulnerability. PHP_SELF contains user-supplied data and must be sanitized before use. Think about what happens when I tell by browser to hit http://example.com/path/to/yourscript.php/"><script>alert('owned');<%2fscript><junk junk="
posted by Rhomboid at 3:40 PM on June 21, 2011


The function get_header() will always call into itself the theme file "header.php" (docs).

You mention you have a header called 'header-sub.php'.

To call that, you would instead call:

get_header('sub');

So this would replace your calls to get_header()
if ( is_home() ) :
  get_header();
else :
  get_header('sub');
endif;
I personally on my blog have code to call in headers that are named like header-20050505.php and so for archival pages I can show appropriate headers in context. I use a different technique there, I basically call file_get_contents() after first figuring out what the correct dated header would be. Let me know if that code would be helpful to you.
posted by artlung at 10:07 PM on June 21, 2011


« Older Take me around the world ...   |   Have an old letter - need help transcribing a few... Newer »
This thread is closed to new comments.