Sha*_*rif 1 php wordpress image shortcode
我是一个新手,请帮助我创建一个 wordpress 图片短代码,就像这样简单:
[img src=""]
Run Code Online (Sandbox Code Playgroud)
它显示了它的缩略图(缩略图宽度=100%),链接到 OR 时打开相同的源图像,单击时。
我尝试搜索但在现有插件中找不到,如果有请指导我。
请彻底指导我在 function.php 或其他任何地方复制粘贴。
// Add Shortcode
function img_shortcode($atts)
{
// Attributes
$atts = shortcode_atts(
[
'src' => '',
'link_to_img' => '',
], $atts, 'img'
);
$return = '';
if ($atts['link_to_img'] == 'yes')
{
$return = '<a href="' . $atts['src'] . '">
<img src="' . $atts['src'] . '"/>
</a>';
}
else{
$return = '<img src="' . $atts['src'] . '"/>';
}
// Return HTML code
return $return;
}
add_shortcode('img', 'img_shortcode');
Run Code Online (Sandbox Code Playgroud)
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
USAGE
Without link:: In PHP
echo do_shortcode('[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="no"]');
Run Code Online (Sandbox Code Playgroud)
Without link:: In Editor
[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="no"]
Run Code Online (Sandbox Code Playgroud)
With link:: In PHP
echo do_shortcode('[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="yes"]');
Run Code Online (Sandbox Code Playgroud)
With link:: In Editor
[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="yes"]
Run Code Online (Sandbox Code Playgroud)
Hope this helps!
| 归档时间: |
|
| 查看次数: |
17141 次 |
| 最近记录: |