Products
GG网络技术分享 2025-03-18 16:14 0
我们在日常的WordPress文章文章编辑中,遇到好看的站点或者模板,需要添加图片,今天技术宅教大家免插件实现WordPress 文章中自动获取网站截图,这里用到的其实是WordPress短代码功能!
添加代码
function wp_screenshot($atts, $content=null){
extract(
shortcode_atts(
array(
\"shots\" => \"https://s0.wordpress.com/mshots/v1/\",
\"url\" => \"https://www.wpbeginner.com\",
\"width\" => \"600\",
\"height\" => \"450\",
\"alt\" => \"screenshot\"
),
$atts
)
);
$img = \'<div class=\"wp-shot\"><a href=\"\' . $url . \'\" rel=\"external nofollow\" target=\"_blank\"><img src=\"\' . $shots . \'\' . urlencode($url) . \'?w=\' . $width . \'&h=\' . $height . \'\" alt=\"\' . $alt . \'\" /></a></div>\';
return $img;
}
add_shortcode(\"shot\", \"wp_screenshot\");
使用方法
发表文章时,添加短代码:
[shot url=\"https://jszbug.com/\"]
也可以自定义截图大小:
[shot url=\"https://jszbug.com/\" width=\"600\" height=\"450\"]
效果截图
直接添加链接
这个方法使用更加方便,获取截图的基本格式:
https://s0.wordpress.com/mshots/v1/https://jszbug.com/?w=600&h=450
也可以直接将上述地址链接格式像正常插入图片(从URL插入)一样添加到文章中,上面的截图就是这么加的,并没有用短代码。
因需要即时生成截图,第一次打开可能看不到图片,刷新一下就可以了,不行就多刷新几次。生成的截图存储在WordPress.com服务器上,不会占用自己的空间和资源,不过加载可能有些慢。
下面给大家介绍如何在 WordPress 文章中自动获取网站截图,希望对需要的朋友有所帮助!
一、短代码
将下面的代码添加到当前主题函数模板 functions.php 中:
function wp_screenshot($atts, $content=null){ extract( shortcode_atts( array( \"shots\" => \"https://s0.wordpress.com/mshots/v1/\", \"url\" => \"https://www.wpbeginner.com\", \"width\" => \"600\", \"height\" => \"450\", \"alt\" => \"screenshot\" ), $atts ) ); $img = \'<div class=\"wp-shot\"><a href=\"\' . $url . \'\" rel=\"external nofollow\" target=\"_blank\"><img src=\"\' . $shots . \'\' . urlencode($url) . \'?w=\' . $width . \'&h=\' . $height . \'\" alt=\"\' . $alt . \'\" /></a></div>\'; return $img; } add_shortcode(\"shot\", \"wp_screenshot\"); |
发表文章时,添加短代码:
[shot url=\"http://zmingcx.com/\"] |
或者自定义截图大小
[shot url=\"http://zmingcx.com/\" width=\"600\" height=\"450\"] |
修改其中的网址链接链接即可。
具体效果
二、直接添加链接
这个方法使用更加方便,获取截图的基本格式:
https://s0.wordpress.com/mshots/v1/http://zmingcx.com/?w=600&h=450 |
也可以直接将上述地址链接格式像正常插入图片(从URL插入)一样添加到文章中,上面的截图就是这么加的,并没有用短代码。
因需要即时生成截图,第一次打开可能看不到图片,刷新一下就可以了,不行就多刷新几次。生成的截图存储在WordPress.com服务器上,不会占用自己的空间和资源,不过加载可能有些慢。
以上就是在 WordPress 文章中自动获取网站截图的详细内容,更多请关注网站的其它相关文章!
Demand feedback