最合适的回答,由SO网友:Md. Amanur Rahman 整理而成
好吧伙计们。。我终于用下面的代码得到了它。
function wpse55748_filter_post_thumbnail_html( $html ) {
// If there is no post thumbnail,
// Return a default image
global $post;
$pID = $post->ID;
$thumb = \'large\';
$imgsrc = FALSE;
if (has_post_thumbnail()) {
$imgsrc = wp_get_attachment_image_src(get_post_thumbnail_id($pID),$thumb);
$imgsrc = $imgsrc[0];
} elseif ($postimages = get_children("post_parent=$pID&post_type=attachment&post_mime_type=image&numberposts=0")) {
foreach($postimages as $postimage) {
$imgsrc = wp_get_attachment_image_src($postimage->ID, $thumb);
$imgsrc = $imgsrc[0];
}
} elseif (preg_match(\'/<img [^>]*src=["|\\\']([^"|\\\']+)/i\', get_the_content(), $match) != FALSE) {
$imgsrc = $match[1];
}
if($imgsrc) {
$imgsrc = \'<img src="\'.$imgsrc.\'" alt="\'.get_the_title().\'" class="summary-image" />\';
$html = $imgsrc;
}
if ( \'\' == $html ) {
return \'<img src="\' . get_template_directory_uri() . \'/images/default-featured-image.png" />\';
}
// Else, return the post thumbnail
return $html;
}
add_filter( \'post_thumbnail_html\', \'wpse55748_filter_post_thumbnail_html\' );
在这里,我首先用regex搜索帖子内容,如果内容有任何类型,如果