wordpress自动获取文章内图片为特色图像

缩略图可以来自featured图片,也可以来自文章内图片,还可以在前两者均无时随机显示一些图片,接下来就介绍如何修改主题模板实现为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, 20); echo get_bloginfo ( 'stylesheet_directory' ); echo '/images/random/tb'.$random.'.jpg'; } return $first_img; }

在主题目录images文件夹下建立random文件夹,里面存入20张图片,命名tb1-20,jpg格式

消息盒子
# 您有11条未读消息 #
# 您需要首次评论以获取消息 #
# 您需要首次评论以获取消息 #

只显示最新10条未读和已读信息