wordpress使用文章缩略图
首先检测有没有设置缩略图,如没有则查找文章内容页有无图片,如没有则调用指定图片
functions.php
add_theme_support( 'post-thumbnails' );
function get_post_img_url($thumbnail = true) {
global $post;
if (has_post_thumbnail ()) {
$domsxe = simplexml_load_string ( get_the_post_thumbnail () );
$thumbnailsrc = $domsxe->attributes()->src;
return $thumbnailsrc;
}elseif ($thumbnail) {
$content = $post->post_content;
preg_match_all ( '/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER );
$n = count ( $strResult [1] );
if ($n > 0) {
return $strResult [1] [0] ;
} else {
return trailingslashit( get_template_directory_uri() ) . 'images/c1.jpg';
}
}else {
return trailingslashit( get_template_directory_uri() ) . 'images/c1.jpg';
}
}
页面内调用图片路径
<?php echo get_post_img_url(true); ?>