Blog

How To Set A Default Featured Image For Your Posts And Pages In WordPress Using Advanced Custom Fields

Free Web Hosting

They say pictures are worth a thousand words. That might be a slight exaggeration, but images can be vitally important for websites, particularly for layouts that use featured images. It’s common for themes to display grids or lists of posts, and if some of the posts don’t include a featured image the site can look broken.

One way to avoid the issue of posts missing featured images is to set a default featured image. This way, if a post is mistakenly published without a featured image, it will display the default image instead. This prevents inconsistent layouts by making sure a featured image is always displayed.

My preferred method of managing custom fields is the Advanced Custom Fields plugin, however this method will work with any custom fields plugin, custom settings pages or the Customizer.

To set up default featured images, first you’ll need a settings page. This settings page will include a group with an image field where we can set our default featured image.

If you’re not familiar with creating options pages, use the code below to automatically create an options page with the field for you. Paste it in your functions.php file. Otherwise, set up an options page with a field group that includes an image field. Make sure the name of the image field is default_featured_image.

<?php
function cameronjonesweb_acf_setup() {
acf_add_options_page(
array(
'page_title' => 'Theme Settings',
'menu_slug' => 'cameronjonesweb-theme-settings',
)
);
acf_add_local_field_group(
array(
'key' => 'group_cameronjonesweb_theme_settings',
'title' => 'Theme Settings',
'fields' => array(
array(
'key' => 'field_cameronjonesweb_default_image',
'label' => 'Default Featured Image',
'name' => 'default_featured_image',
'type' => 'image',
),
),
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'cameronjonesweb-theme-settings',
),
),
),
)
);
}
add_action( 'acf/init', 'cameronjonesweb_acf_setup' );
view raw acf-options.php hosted with ❤ by GitHub

Now that we have a custom field for the default featured image, go ahead and pick the image to use.

The next step is to hijack the functions WordPress uses to get the featured image for each post. At it’s core, the various different functions all rely on get_post_thumbnail_id() to get the ID of the attachment used for the featured image. What we will do is make this function return the ID of our default featured image if there isn’t one set. Using this approach means that it should work with most themes, and you won’t need to go through your templates and replace all the functions being used to get the featured image.

Paste the below code into your functions.php file. If you’re using a method other than ACF, be sure to replace the get_field() call on line 12 to get the ID of your featured image.

Your site should now display your default featured image for any post that doesn’t have one set.

Did you find this post useful?

YesNo