批量取消特色图片

以下代码加入主题 Functions.php 文件中,即可将所有文章的特色图片取消


global $wpdb;
$wpdb->query( "
    DELETE FROM $wpdb->postmeta 
    WHERE meta_key = '_thumbnail_id'
" );

或者

// WordPress 快速移除所有文章的特色图片
delete_post_meta_by_key( '_thumbnail_id' );

没有图片的文章固定一个图片为特色图片,其中444是图片ID

function wpforce_featured() {
    global $post;
    $already_has_thumb = has_post_thumbnail($post->ID);
    if (!$already_has_thumb)  {
        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
        if ($attached_image) {
            foreach ($attached_image as $attachment_id => $attachment) {
                set_post_thumbnail($post->ID, $attachment_id);
            }
        } else {
            set_post_thumbnail($post->ID, '414');
        }
    }
}  //end function
add_action('the_post', 'wpforce_featured');
add_action('save_post', 'wpforce_featured');
add_action('draft_to_publish', 'wpforce_featured');
add_action('new_to_publish', 'wpforce_featured');
add_action('pending_to_publish', 'wpforce_featured');
add_action('future_to_publish', 'wpforce_featured');
大佬论坛

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code