Skip Navigation

[Resolved] Code to display custom image size and alt text

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by mattB-2 9 years, 4 months ago.

Assisted by: Waqas.

Author
Posts
#270804

Hi,

I know the code for displaying a custom image size from the image field and I also know the code for pulling through the alt text. I would like to combine these two so I can use a custom image size and pull through the alt text.

I'm using the code below which I found on another forum post.

add_shortcode('cs-before-images', 'cs_images_shortcode');
function cs_images_shortcode() {
global $post, $wpdb;
 
$images = get_post_meta($post->ID, 'wpcf-before_images', false);
$out = '';
foreach ($images as $image) {
$attachment_id = $wpdb->get_var($wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid = %s",
$image
));
$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
$out .= '<img src="' . $image . '" alt="' . $alt . '" title="' . $alt . '" />';
}
$out .= '';
return $out;
}

<?php echo do_shortcode('[cs-before-images]'); ?>

This works perfectly for the alt text but what I can't figure out is how to use this code to also display the custom image size that I have created in my functions.php file using add_image_size.

Any help would be greatly appreciated.

Matt

#270910

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Please consider your code as altered below:

add_shortcode('cs-before-images', 'cs_images_shortcode');
function cs_images_shortcode() {
	global $post, $wpdb;
	  
	$images = get_post_meta($post->ID, 'wpcf-before_images', false);
	$out = '';

	foreach ($images as $image) {
		$attachment_id = $wpdb->get_var($wpdb->prepare(
			"SELECT ID FROM $wpdb->posts WHERE guid = %s",
			$image
		));
		
		$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
		$img = wp_get_attachment_image_src($attachment_id, "thumbnail");
		$out .= '<img src="' . $img[0] . '" alt="' . $alt . '" title="' . $alt . '" />';
	}
	
	$out .= '';
	
	return $out;
}

Please notice wp_get_attachment_image_src() which retrieves the target image with specified size. For more information, please see http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

#271516

Hi Waqas,

Thank you so much for that code. It worked perfectly.

Thanks again, Matt

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.