问题
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?
回答1:
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');
来源:https://stackoverflow.com/questions/14702782/wordpress-featured-image-custom-shortcode