Title: Dynamic header image using custom fields&#8230;
Last modified: August 19, 2016

---

# Dynamic header image using custom fields…

 *  [echstudios](https://wordpress.org/support/users/echstudios/)
 * (@echstudios)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/dynamic-header-image-using-custom-fields/)
 * I’d like to set up my pages to display a unique header image on based on their
   page ID.
 * Is it possible to use the “get_post_meta” call within my header.php file?
 * If so, seems like I could set up my pages custom fields to specify their own 
   image. Will this work?

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [churchthemer](https://wordpress.org/support/users/churchthemer/)
 * (@churchthemer)
 * [17 years, 7 months ago](https://wordpress.org/support/topic/dynamic-header-image-using-custom-fields/#post-876628)
 * I think you’d be better off using the queried object’s slug and then using a 
   set of if statements (or better, switch statements) to check the slug and return
   the correct header image.
 * Place the code below in your header and it will print out the slug name for each
   page. It’s just for testing, once you are satisfied that its returning the right
   slug names remove the echo $slug; line.
 *     ```
       <?php
           $post_obj = $wp_query->get_queried_object();
           $slug = $post_obj->post_name;
           echo $slug;
       ?>
       ```
   
 * then you can set a series of checks to return the right image
 *     ```
       if($slug == "about"){
               echo "<img src=\"about-header.jpg\">";
           }
           else if($slug == "contact"){
               echo "<img src=\"contact-header.jpg\">";
           }
           else if($slug == "news"){
               echo "<img src=\"news-header.jpg\">";
           }
           else{
               echo "<img src=\"default-header.jpg\">";
           }
       ```
   
 * You can output to an <img> tag like shown above or to a css background property.
 * Hope that helps.
 *  [planetrussell](https://wordpress.org/support/users/planetrussell/)
 * (@planetrussell)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/dynamic-header-image-using-custom-fields/#post-876824)
 * Wow. This is the first thorough, logical answer to this question I’ve found to
   date.
 * Given that displaying a different header graphic for different WP categories 
   would seem to be a perfectly sensible, useful thing for many WP bloggers, it’s
   amazing that no one appears to have implemented this as a plugin to date.
 * Or have they? If there’s a plugin to do this, please advise who/what/where? Thanks.
 *  [quickactionguides](https://wordpress.org/support/users/quickactionguides/)
 * (@quickactionguides)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/dynamic-header-image-using-custom-fields/#post-876854)
 * Hi,
 * I tried using the code below and uploaded some images with the relevant file 
   names into the themes image folder …
 * <?php
    $post_obj = $wp_query->get_queried_object(); $slug = $post_obj->post_name;
   if($slug == “about”){ echo “<img src=\”about-header.jpg\”>”; } else if($slug 
   == “contact”){ echo “<img src=\”contact-header.jpg\”>”; } else if($slug == “news”){
   echo “<img src=\”news-header.jpg\”>”; } else{ echo “<img src=\”default-header.
   jpg\”>”; } ?>
 * It didn’t work in WordPress 2.7 so I guess you have to disable the following 
   file somehow?
 * wp-content/themes/default/header-image.php
 * I tried deleting this file and you get a blank white header.
 * Is there anyway you can put some code in the header-image.php to disable it and
   allow the dynamic header code to work?
 *  [pequ76](https://wordpress.org/support/users/pequ76/)
 * (@pequ76)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/dynamic-header-image-using-custom-fields/#post-876857)
 * it’s likely that you need to write it like this:
 * `<img src="<?php bloginfo('template_url'); ?>/img_name.jpg" />`
 *  [wadelovesrachel](https://wordpress.org/support/users/wadelovesrachel/)
 * (@wadelovesrachel)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/dynamic-header-image-using-custom-fields/#post-876883)
 * I also wanted to use the custom fields to do this. I didn’t find anything pre-
   made, so I came up with this.
 *     ```
       <?php $page_header_image = "path/to/images/folder/".get_post_meta($post->ID, 'header_img', 1);
       if( strlen($page_header_image) < strlen(path/to/images/folder/))
       {$page_header_image = "path/to/images/folder/default.jpg";}
       ?>
   
       <img src="<?php echo $page_header_image;?>">
       ```
   
 * I have a bit more to do to clean this up, but it is basically working for me.
 * For this to work, you have to add a custom field with a key of header_img and
   the value for that key can be set to the name of the image you would like to 
   use.
 * I included the path in the code instead of the meta tag simply because that was
   what worked best for me on this specific project. I plan on updating that later,
   but wanted to get this up.
 * I also have a test to basically make sure there is an image of some sort to display.
   If the meta tag comes back empty then a default image is shown.
 * Hope that helps anyone else searching like I was.
 *  [k0pper](https://wordpress.org/support/users/k0pper/)
 * (@k0pper)
 * [17 years ago](https://wordpress.org/support/topic/dynamic-header-image-using-custom-fields/#post-876888)
 * We have developed plugin that accomplished some of this functionality.
 * While it doesn’t allow you to set a header image for each category (yet), it 
   does allow you to set a different header image for each page/post, as well as
   a default header.
 * [http://wordpress.org/extend/plugins/dynamic-headers/](http://wordpress.org/extend/plugins/dynamic-headers/)

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Dynamic header image using custom fields…’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [header](https://wordpress.org/support/topic-tag/header/)
 * [image](https://wordpress.org/support/topic-tag/image/)
 * [Page ID](https://wordpress.org/support/topic-tag/page-id/)

 * 6 replies
 * 7 participants
 * Last reply from: [k0pper](https://wordpress.org/support/users/k0pper/)
 * Last activity: [17 years ago](https://wordpress.org/support/topic/dynamic-header-image-using-custom-fields/#post-876888)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
