WordPress技巧:主动显示文章第一张图片
摘要:WordPress技巧:主动显示文章第一张图片
WordPress技巧:主动显示文章第一张图片
在修正主题的时分,咱们能够需求WordPress主动获取文章第一张图片,在博客吧看到了相干的完成方法,记载一下,预计当前也用失去。
在以后利用的主题模板的functions.php文件<?php和?>之前减少以下代码
function catch_that_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)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
在以后主题模板的index.php文件的内容代码前或后减少以下代码
<?php echo catch_that_image() ?>
当然了,要限度显示图片的效果(比如大小、边框),就需求你利用css设置了。