Wordpress 特色图片自定义简码 [英] Wordpress Featured Image Custom Shortcode

查看:52
本文介绍了Wordpress 特色图片自定义简码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个让我头疼的问题.我正在尝试在我的主题 functions.php 文件中创建自定义短代码,这将允许我将帖子特色图片插入并放置到帖子中并对齐它从左到右.

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');

我哪里出错了?

推荐答案

哎呀...短代码功能可以永远不要打印任何东西.您已经返回结果!

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');

这篇关于Wordpress 特色图片自定义简码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆