Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
excerpt from wordpress header.php to have it get a category based header image
<?PHP
// Check if this is a post or page, if it has a thumbnail, and if it's a big one
if ( is_singular() &&
has_post_thumbnail( $post->ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( HEADER_IMAGE_WIDTH, HEADER_IMAGE_WIDTH ) ) ) &&
$image[1] >= HEADER_IMAGE_WIDTH ) :
// Houston, we have a new header image!
echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
//--- Added to have blog category page headings
elseif( is_category() ) :
$cat = get_query_var('cat');
$yourcat = get_category ($cat);
$src = get_stylesheet_directory_uri().'/images/headers/'.$yourcat->slug.'_header.jpg';
$loc = get_stylesheet_directory().'/images/headers/'.$yourcat->slug.'_header.jpg';
$dflt = get_stylesheet_directory_uri().'/images/headers/blog_header.jpg';
if (file_exists($loc)) :
echo '<img src="'.$src.'" width"'.HEADER_IMAGE_WIDTH.'" height="'.HEADER_IMAGE_HEIGHT.'alt="" />';
else :
echo '<img src="'.$dflt.'" width"'.HEADER_IMAGE_WIDTH.'" height="'.HEADER_IMAGE_HEIGHT.'alt="" />';
endif;
// end of blog category page headings
//--- Added to have posts show their category image if none other available.
elseif(is_single()) :
$category = get_the_category();
$kat = $category[0]->category_nicename;
$src = get_stylesheet_directory_uri().'/images/headers/'.$kat.'_header.jpg';
$loc = get_stylesheet_directory().'/images/headers/'.$kat.'_header.jpg';
$dflt = get_stylesheet_directory_uri().'/images/headers/blog_header.jpg';
if (file_exists($loc)) :
echo '<img src="'.$src.'" width"'.HEADER_IMAGE_WIDTH.'" height="'.HEADER_IMAGE_HEIGHT.'alt="" />';
else :
echo '<img src="'.$dflt.'" width"'.HEADER_IMAGE_WIDTH.'" height="'.HEADER_IMAGE_HEIGHT.'alt="" />';
endif;
// end of post category heading
//------ blog home page setting
elseif(is_home()) :
echo '<img src="'.get_stylesheet_directory_uri().'/images/headers/blog_header.jpg" width"'.HEADER_IMAGE_WIDTH.'" height="'.HEADER_IMAGE_HEIGHT.'alt="" />';
else : ?>
<img src="<?php header_image(); ?>" width="<?php echo HEADER_IMAGE_WIDTH; ?>" height="<?php echo HEADER_IMAGE_HEIGHT; ?>" alt="" />
<?php endif; // end check for featured image or standard header ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.