【发布时间】:2017-02-27 06:19:00
【问题描述】:
我是新手,请帮我创建一个wordpress图像简码,简单如下:
[img src=""]
其中显示其缩略图(缩略图宽度=100%),链接到或打开相同的源图像,单击时。
我尝试搜索但在现有插件中找不到,如果有请指导我。
请彻底指导我在 function.php 或其他任何地方的每个复制粘贴。
【问题讨论】:
标签: php wordpress image shortcode
我是新手,请帮我创建一个wordpress图像简码,简单如下:
[img src=""]
其中显示其缩略图(缩略图宽度=100%),链接到或打开相同的源图像,单击时。
我尝试搜索但在现有插件中找不到,如果有请指导我。
请彻底指导我在 function.php 或其他任何地方的每个复制粘贴。
【问题讨论】:
标签: php wordpress image shortcode
// 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');
代码进入您的活动子主题(或主题)的 function.php 文件中。或者也可以在任何插件 php 文件中。
用法
没有链接:: 在 PHP 中
echo do_shortcode('[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="no"]');
没有链接:: 在编辑器中
[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="no"]
带有链接:: 在 PHP 中
echo do_shortcode('[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="yes"]');
有链接:: 在编辑器中
[img src="http://example.com/wp-content/uploads/2017/02/hello.jpg" link_to_img="yes"]
希望这会有所帮助!
【讨论】:
图库功能允许 wordpress 使用简单的简码向您的帖子和页面添加一个或多个图片库
[gallery ids="729,732,731,720"]
【讨论】: