你想要的has_post_thumbnail
.
add_filter( \'the_content\', insert_featured_image, 20 );
function insert_featured_image( $content ) {
global $post;
if (has_post_thumbnail($post->ID)) {
$content = preg_replace( "/<\\/p>/", "</p>" . get_the_post_thumbnail($post->ID, \'post-single\'), $content, 1 );
}
return $content;
}
注:以上代码来自
post referenced in the OP, 但在
has_post_thumbnail
有条件的我还必须补充
global $post;
避免“未定义变量”
Notices
s、 或者,你可以离开
$post
引用输出为
has_post_thumbnail
和
get_the_post_thumbnail
将承担全球
$post
(这就是为什么原始代码可以工作的原因)。
add_filter( \'the_content\', insert_featured_image, 20 );
function insert_featured_image( $content ) {
if (has_post_thumbnail()) {
$content = preg_replace( "/<\\/p>/", "</p>" . get_the_post_thumbnail(null, \'post-single\'), $content, 1 );
}
return $content;
}