Different header image for each page in wordpress

HI, I am new to this forum. I came across an old thread on this subject - I needed a different header for each page on my wp site, but it didn’t help me much, mostly due to my lack of knowledge on the php subject. I found this code somewhere along the line in my days of searching for an answer, that seemed to work…at first

<?php if (is_front_page()) { ?>

<div id=“header”>

<?php } else { ?>
<div id="<?php echo $post->post_name; ?>

then I created all the different page divs in my CSS file using their page name id’s.
it works great for all the pages, but then I discovered that when I do a search on the site, there is no header image for any searched items because I haven’t allocated a div but each search has a different page name like '?=“whatever is typed”

can anyone shed some light on what I am supposed to call the div for a general search page…if there is such a thing? or am I going about this all the wrong way

Any light would be most appreciated

Not sure if this will work, because I don’t know WP that well, but try:

if($post AND $post->post_name) {
   echo "<div id='{$post->post_name}'>";
else {
   echo "<div id='header'>";

That should default to #header, unless there is a post_name

Thanks for this!

I tried that code instead of what i had and it changed the headers for all the pages except the home page, there was nothing and then when I did a search it put the original header image in the body and the image from the page I searched from in the correct place!?

this Idea made me think of another way to maybe do it - leave the original code and just call 7 different images to the 7 pages I want with different header images. Any Idea how that should look? I tried something like below but I have syntax errors, dont think I am closing the tags properly:confused: Also I think I will land up with the same search page problems…sigh

<?php if (is_front_page()) { ?> 
  <div id="header">

<?php } elseif (is_accommodation()) { ?>
<div id='accommodation'>...</div>

Is accommodation the page slug name? Use is_page(‘xxx’)
xxx = id, title or slug

yes, accommodation is the page slug name as I am using permalinks note page id’s.
Seems as I have solved the problem!!! I cant express my happiness right now.

Turns out I added a file called search.php to define how the search pages looked and that was causing all the chaos - so I just deleted it and now my search quires work except for - not found!? This has no header image at all?

ps, this means that this code actually does work, its just not idiot proof :smiley:

<?php if (is_front_page()) { ?>

<div id="header"> 

<?php } else { ?> 
<div id="<?php echo $post->post_name; ?>

just need to figure out why the search-not found page has no header?

also is_front_page is not a valid function. use is_home()
search.php is an included file in the core package of wordpress, it is not found because you deleted search.php. there are ways to direct search elsewhere i believe, but don’t want to dig around how.

searches are not posts, they’re pages, therefore have no post_name

My theme never had a search.php, I just added that when I was trying another solution for my search page problem, which I then forgot about but my search pages are now fixed, just the “not found” search result that has no image

When I added is_home instead of is_front_page then the home page image disappears.

I see now what it is doing; when i do a search for a word that is found on my site then it uses the header of the first page that comes up in the search results, ie if I search for gallery then it uses the gallery pages image - so when I type in a word that is not found in the site it cant find a header image cause there is no page associated with the searched word. Maybe I should just live with that, cause if it cant be found then I guess the searcher is in the wrong place to start :slight_smile:

Ah ok. The is_home and is_front thing is weird. Says to me that settings are off somewhere.

i am guessing you are trying words in the titles posts as search criteria.