Wordpress Featured Image Custom Shortcode

两盒软妹~` 提交于 2019-12-11 15:07:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!