售前咨询
技术支持
渠道合作

小优化:WordPress文章没有图片时显示随机图片或默认图片

提高文章的收录机率 ?百度的图片位于其八大目标导航之列,可见其对图片也赋予了较高的权重。视频网站中的图片都能得到很好的排名就是其最有力的体现。既然图片带来如此好的优化效果,那我们何不将其纳入文章中呢。文章中加入图片一方面增强了读者的阅读体验,另一方面也可以增加文章的转载量。而这两项就足以提高文章的收录了。特别是现在百度越来越重视用户体验这一环节了,因此,在这个有利的环境下做出对的决策那对文章优化是非常有利的。纯文字的长篇大论总显得枯燥乏味,今天数安时代小编将给大家分享一下WordPress文章没有图片时显示随机图片或默认图片。
1、WordPress文章没有图片时显示随机图片方法如下:
FTP工具连接php空间找到Wordpress主题目录内文件:functions.php插入下列代码:

//支持外链缩略图
if ( function_exists('add_theme_support') )
add_theme_support('post-thumbnails');
function catch_first_image() {global $post, $posts;$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
$random = mt_rand(1, 10);
echo get_bloginfo ( 'stylesheet_directory' );
echo '/images/random/'.$random.'.jpg';
}
return $first_img;
}
;

//上面的代码,只需要准备10张图片,放在主题的/images/random/目录即可
2、WordPress文章没有图片时显示默认图片方法如下:
FTP工具连接php主机找到Wordpress主题目录内文件:functions.php插入下列代码:

//支持外链缩略图
if ( function_exists('add_theme_support') )
add_theme_support('post-thumbnails');
function catch_first_image() {global $post, $posts;$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
$first_img = bloginfo('template_url'). '/images/default-thumb.jpg';
}
return $first_img;
}
;

注意事项:以上的函数,在调用的时候,可以使用下面的语句

<?php echo catch_first_image() ?>

 

上一篇:

下一篇:

相关新闻

 

领取优惠
免费预约

申请试用SSL证书

提交成功!

咨询客服