Horje
Custom headers and custom image sizes - Wordpress Solution
In my theme I have add_theme_support('custom-header') and add_image_size('hero', 435, 290, true). When I output my custom header in a template file I would like to show the 'hero' version (435x290) of my custom header, not the original, uploaded image. Please can somebody help me write a function to accomplish the above. Many thanks.

Solution - 1

The code below is not tested... Have a backup of your own and test its working...

<?php
$custom_header = get_custom_header();
$hero_src = wp_get_attachment_image_src(intval($custom_header->attachment_id), 'hero', false);

if(isset($hero_src[0])) {
?>
<div class="hero" style="background:#ddd url(<?php echo $hero_src[0]; ?>) no-repeat center center;">
<?php } else { ?>
<div class="hero" style="background:#ddd url("Add default image url here") no-repeat center center;">
<?php } ?>


Solution - 2

Try this:

<?php
	if (has_post_thumbnail( $post->ID ) ):
	$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'hero' ); ?>
		<img src="<?php echo $image[0]; ?>" />
<?php endif; ?>
It will check if the post has a thumbnail, Then if it does, will show the image.


Solution - 3

try this :

<?php
	$header_image = get_header_image();
	if ($header_image):
			if (function_exists('get_custom_header'))
				{
					$header_image_width = get_theme_support('custom-header', 'width');
				}
			else
				{
					$header_image_width = HEADER_IMAGE_WIDTH;
				} ?>
			<a rel="nofollow" href="<?php echo esc_url(home_url('/'));?>">
				<?php
			if (is_singular() && has_post_thumbnail($post->ID) && ($image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(
					$header_image_width,
					$header_image_width
			))) && $image[1] >= $header_image_width):
					echo get_the_post_thumbnail($post->ID, 'hero');
			else:
					if (function_exists('get_custom_header'))
						{
							$header_image_width  = get_custom_header()->width;
							$header_image_height = get_custom_header()->height;
						}
					else
						{
							$header_image_width  = HEADER_IMAGE_WIDTH;
							$header_image_height = HEADER_IMAGE_HEIGHT;
						} ?>
					<img src="<?php
					header_image(); ?>" width="<?php echo $header_image_width;?>" height="<?php echo $header_image_height; ?>" alt="" />
			<?php endif; ?>
			</a>
			<?php endif; ?>
don't forget to regenerate thumbnails : http://wordpress.org/extend/plugins/regenerate-thumbnails/ after add/remove custom image size :) hope this help, :)


Solution - 4

Put this in your functions.php add_theme_support( 'post-thumbnails' ); add_image_size( 'hero', 200, 200, true ); // Set hero post thumbnail class, true param means crop then in your template something like 'hero')); ?> Or for attachment you can do the following which is what i use

<?php $image_id = get_post_thumbnail_id(); // attachment ID
	$image_attributes = wp_get_attachment_image_src( $image_id, 'hero' ); // returns an array
?> 
and to retrieve the attachment
<a rel="nofollow" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<img class="hero" src="<?php echo $image_attributes[0]; ?>">
Forget the above i misread the question. Here are some references. Custom Header This feature enables Custom_Headers support for a theme as of Version 3.4. add_theme_support( 'custom-header' ); Note that you can add default arguments using: $defaults = array( 'default-image' => '', 'random-default' => false, 'width' => 0, 'height' => 0, 'flex-height' => false, 'flex-width' => false, 'default-text-color' => '', 'header-text' => true, 'uploads' => true, 'wp-head-callback' => '', 'admin-head-callback' => '', 'admin-preview-callback' => '', ); add_theme_support( 'custom-header', $defaults ); Source: http://codex.wordpress.org/Custom_Headers


Solution - 5

Hi designbuildtest, Please test this code, from Twenty Eleven's header.php, changed to use 'hero' size.


						// Check to see if the header image has been removed
						$header_image = get_header_image();
						if ( $header_image ) :
							$header_image_width  = get_custom_header()->width;
					?>
					<a rel="nofollow" href="<?php echo esc_url( home_url( '/' ) ); ?>">
						<?php
							// The header image
							// 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, 'hero' );
							else : ?>
							<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />
						<?php endif; // end check for featured image or standard header ?>
					</a>
					<?php endif; // end check for removed header image 

Regards, Gabriel





Wordpress

Related
How do I set SITE_URL to override the database url info?  - Wordpress Solution How do I set SITE_URL to override the database url info? - Wordpress Solution
How do I make custom post types not appear as blog posts? - Wordpress Solution How do I make custom post types not appear as blog posts? - Wordpress Solution
Redirect exit page: Get external url from custom field - Wordpress Solution Redirect exit page: Get external url from custom field - Wordpress Solution
Problem with headway theme css - Wordpress Solution Problem with headway theme css - Wordpress Solution
Related posts within a taxonomy. - Wordpress Solution Related posts within a taxonomy. - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8