您可以在本教程中找到答案:How to Set a Default Fallback Image for WordPress Post Thumbnails
将此添加到functions.php
在主题文件夹中:
add_theme_support( \'post-thumbnails\' );
function myprefix_main_image() {
$attachments = get_children( \'post_parent=\'.$post->ID.\'&post_type=attachment&post_mime_type=image&order=desc\' );
if( $attachments ) {
$keys = array_reverse( $attachments );
set_post_thumbnail( $post->ID, $keys[0]->ID );
};
}
在模板中,您要在其中显示帖子图像:
<?php if ( (function_exists( \'has_post_thumbnail\') ) && ( has_post_thumbnail() ) ) {
echo get_the_post_thumbnail( $post->ID );
} else {
myprefix_main_image();
echo get_the_post_thumbnail( $post->ID );
} ?>
EDIT : 好多了,谢谢Chip Bennett