wordpass 首页缩略图的调用方法

作者:小强建站2022-01-03 点击量:
wordpress模板中找到模板函数文件functions.php;
在functions.php粘贴下面的缩略图代码:

//调用缩略图
function get_first_image() {
global $post;
$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)){ //Defines a default image
$first_img = bloginfo('template_url') . "/default.jpg";
};
return $first_img;
}
在网站模板的任意一个模板中,只要需显示缩略图片位置,使用下面的调用代码来调用缩略图。

<?php if ( has_post_thumbnail() ) { the_post_thumbnail(array(296,296),array('alt'=> trim(strip_tags( $post->post_title ))));} else {?><img src="<?php echo get_first_image(); ?>" alt="<?php the_title(); ?>" width="296" height="296"/><?php }?>

上面代码里的29,296为图片的尺寸,可以根据自己网站的需要进行修改。
在主题文件夹下放一个默认图片,名字为default.jpg。
通过上面的方法,我们自己建网站时,也可以很轻松的调用文章缩略图片了。
在线QQ 联系本人 二维码

电话

15901294411

扫一扫,关注我们