Web前端总结(第四篇)文章缩略图的调用方法(支持文章页调用)
Vue ShanLing(闪灵科技)开发者团队介绍:开发者在实际项目中Vuexls模板如何调用文章缩略图?
//调用缩略图
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;
}
在任意模块中,只要需显示缩略图片位置,使用下面的调用代码来调用缩略图。
<?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 }?>