Home  >  Article  >  CMS Tutorial  >  How to automatically add featured images to WordPress posts

How to automatically add featured images to WordPress posts

藏色散人
藏色散人forward
2019-12-28 11:45:532560browse

The following column WordPress Tutorial will introduce to you how to automatically add featured images to WordPress articles. I hope it will be helpful to everyone’s WordPress imitation siteHelpful!

How to automatically add featured images to WordPress posts

#Featured images in WordPress is a very useful feature that allows you to add a thumbnail image to each post in the post list. However, it is inconvenient to add featured images manually when editing an article. The following code can automatically set the first image in the article as the featured image.

Add the following code to functions.php of the current theme:

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);
            }
        }
    }
}  //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');

What if there are no pictures in the current article but you want to display a default thumbnail? You can add Modify the above code to call a picture in the media library as the default thumbnail:

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');

The number 414 is the ID number of a picture attachment in the media library.

Tips

The above code is just a technical article and may affect previously added featured images, so don’t try it on your own website easily.

Featured images are only suitable for users who don’t care about space traffic and size, because each image will be cropped into multiple thumbnails of different sizes for easy calling in different locations. The most important thing is that it does not support external links, which is very difficult. Waste of space....

The above is the detailed content of How to automatically add featured images to WordPress posts. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:zmingcx.com. If there is any infringement, please contact admin@php.cn delete