[英]WordPress simple Image Shortcode
我是一個新手,請幫助我創建一個 wordpress 圖片短代碼,就像這樣簡單:
[img src=""]
它顯示了它的縮略圖(縮略圖寬度=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');
代碼位於活動子主題(或主題)的 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"]
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.