Wordpress 判断是否设置特色图 有则调用默认特色形象图 没有则调用文章第一张图
//获取第一张图片
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)) {
$first_img = "/img/default-thumb.jpg";
}
// return $first_img;
echo "<img src='";
echo $first_img ;
echo "' alt='";
the_title();
echo "'>";
}
// catch_that_image() ; // 调用
/**
* 获取缩略图,若没有设置,则显示默认第一张图片
* $thumb_size: the thumbnail size name
*/
function xizhitbu_get_thumbnail( $thumb_size ) {
if ( get_the_post_thumbnail() ) {
the_post_thumbnail( $thumb_size );
} else {
// echo "<img src='";
// bloginfo('stylesheet_directory');
// echo "/img/default-thumb.jpg' alt='";
// the_title();
// echo "'>";
catch_that_image() ;
}
}
调用 Wordpress 缩略图
<?php xizhitbu_get_thumbnail('thumb-case'); ?>