lil_bugga
lil_bugga

Reputation: 91

Wordpress Featured Image Custom Shortcode

I've ran into an issue that's doing my head in. I'm trying to create custom shortcodes within my themes functions.php file that will allow me to insert and place a posts featured image into a post and align it either left of right.

Below is the code I tried last, I've been looking at different sources and trying different things to no avail.

function featured_img_left() {
if (has_post_thumbnail() ) {
    $image_id = get_post_thumbnail_id();  
    $image_url = wp_get_attachment_image_src($image_id,'medium');  
    $image_url = $image_url[0]; 
} ?>
<img src="<?php $image_url?>" class="pic_left" />
<?php }
add_shortcode ('feat-img-left', 'featured_img_left');

Where am I going wrong?

Upvotes: 0

Views: 5183

Answers (1)

david.binda
david.binda

Reputation: 1068

Ouch...shortcode function can NEVER print anything. You have tu RETURN the result!

function featured_img_left() {
if (has_post_thumbnail() ) {
    $image_id = get_post_thumbnail_id();  
    $image_url = wp_get_attachment_image_src($image_id,'medium');  
    $image_url = $image_url[0]; 
    $result = '<img src="'.$image_url.'" class="pic_left" />';
    return $result;
}
return;
}
add_shortcode ('feat-img-left', 'featured_img_left');

Upvotes: 6

Related Questions