wordpress调取文章缩略图的方法

wordpress调取文章缩略图的方法:

1、在模板目录下functions.php文件内粘贴以下代码:

//调用缩略图
function get_first_image() {
global $post;
$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 = bloginfo('template_url') . "/default.jpg";
};
return $first_img;
}

 

 2、在网站的模板想要调取文章内图片的缩略图:

<?php echo get_first_image(); ?>

这样就可以在网站首页或是其他想要调取文章内图片缩略图了。

posted @ 2022-10-12 20:18  宽豫轩  阅读(23)  评论(0)    收藏  举报