[WordPress教程] WordPress使用外链图片作特色图 -静鱼客栈

2021-09-29 0 1,212

众所周知,wordpress特色图是不允许外链图片地址的。不知道wordpress官方为何不允许这样做,确实让人很难理解。那该怎么去做呢?

外链图片设置为特色图


// 特色图支持外链
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //定义一个默认图像
$default_img=get_template_directory_uri().'/images/thumbnail.png';
$first_img = "$default_img";
}
return $first_img;
}
 
function mimelove_img($postID) {
 $cti = catch_that_image();
 $showimg = $cti;
 has_post_thumbnail();
 if ( has_post_thumbnail() ) { 
 $thumbnail_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail');
 $shareimg = $thumbnail_image_url[0];
 } else { 
 $shareimg = $showimg;
 };
 return $shareimg;
}

调用方法:


<img src="<?php echo mimelove_img(get_the_ID()); ?>" alt="" >  //当然,你也可以只用php函数

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

静鱼客栈 学习中心 [WordPress教程] WordPress使用外链图片作特色图 -静鱼客栈 https://wp.muooy.cn/9514.html

静鱼客栈的帅逼站长~

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务