首页 > CMS应用 > 正文
wordpress自动添加特色图像

功能简介:对于开启了特色图像的主题自动获取文章的第一张图片为特色图像,无需手动设置!

将一下代码添加至functions.php文件底部,注意php起始!

if ( ! function_exists( ‘fb_set_featured_image’ ) ) {

add_action( ‘save_post’, ‘fb_set_featured_image’ );
function fb_set_featured_image() {

if ( ! isset( $GLOBALS[‘post’]->ID ) )
return NULL;

if ( has_post_thumbnail( get_the_ID() ) )
return NULL;

$args = array(
‘numberposts’ => 1,
‘order’ => ‘ASC’, // DESC for the last image
‘post_mime_type’ => ‘image’,
‘post_parent’ => get_the_ID(),
‘post_status’ => NULL,
‘post_type’ => ‘attachment’
);

$attached_image = get_children( $args );
if ( $attached_image ) {
foreach ( $attached_image as $attachment_id => $attachment )
set_post_thumbnail( get_the_ID(), $attachment_id );
}

}

}

set-featured-image

文章评论

共有 1 条评论

  1. 王莉莉

    我继续顶你!太好的文章了 支持

Top