Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display custom image size for customizer uploaded image WordPress

  • SOLVED

I have a customizer setting for an image on my homepage



$wp_manager->add_setting( 'home_image', array(
'default' => '',
) );

$wp_manager->add_control( new WP_Customize_Image_Control( $wp_manager, 'home_image', array(
'label' => 'Homepage image',
'section' => 'customiser_demo_section',
'settings' => 'home_image',
'priority' => 5
) ) );




// Home image
add_image_size( 'home-image', 768, 9999 );


The image uploads and displays, however I am trying to figure out how to display a custom image size version of the image. My custom image size is called 'home-image' and the following code does not display the correct image size.


<?php
$home_image = get_theme_mod( 'home_image', 'type=image&size=home-image');
echo "\n\t\t\t\t\t<img src='$home_image' />\n";
?>


Wordpress is generating this image correctly in the uploads folder.

Any ideas, how do I display the image size 'home-image' for a customizer image field?

Answers (1)

2014-12-18

Romel Apuya answers:

<?php
echo wp_get_attachment_image( $id, 'your-custom-size' );
?>



you must know the $id of the Media Library image

assuming its 10. so in your code


<?php
echo wp_get_attachment_image( 10, 'home-image' );
?>


Nick comments:

Cool, so how do I get the media ID programmatically ? Can I get it from home_image somehow?


Romel Apuya comments:

its a template right??
you can declare

global $post;

and get the id

$id = get_the_ID();


Romel Apuya comments:

$id = $post->ID;

rather than
$id = get_the_ID();


Nick comments:

That didn't really work fro me, but I have found this little function, that works a treat

https://pippinsplugins.com/retrieve-attachment-id-from-image-url/


Romel Apuya comments:

ok. good to know...