In WordPress, you can set a default featured image for each post category by using a snippet of code in your theme’s functions.php
file. This code checks if a post has a featured image assigned; if not, it sets a default image based on the post’s category.
Code to Assign Default Featured Image by Category
- Open your theme’s
functions.php
file (preferably a child theme to avoid losing changes with updates). - Add the following code, replacing the URLs with your default images for each category:
function set_default_thumbnail($post_id) {
if (has_post_thumbnail($post_id)) {
return;
}
// Define default images for each category
$default_images = [
‘category-1’ => ‘https://your-site.com/wp-content/uploads/category-image1.jpg’,
‘category-2’ => ‘https://your-site.com/wp-content/uploads/category-image2.jpg’,
‘category-3’ => ‘https://your-site.com/wp-content/uploads/category-image3.jpg’,
];
// Get post categories
$categories = get_the_category($post_id);
// Check if categories are assigned and find a matching default image
foreach ($categories as $category) {
if (array_key_exists($category->slug, $default_images)) {
$image_url = $default_images[$category->slug];
// Download image and set as featured image
$image_id = attach_image_from_url($image_url, $post_id);
if ($image_id) {
set_post_thumbnail($post_id, $image_id);
}
break;
}
}
}
add_action(‘save_post’, ‘set_default_thumbnail’);
// Function to download and attach the image to the post
function attach_image_from_url($image_url, $post_id) {
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if (wp_mkdir_p($upload_dir[‘path’])) {
$file = $upload_dir[‘path’] . ‘/’ . $filename;
} else {
$file = $upload_dir[‘basedir’] . ‘/’ . $filename;
}
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null);
$attachment = [
‘post_mime_type’ => $wp_filetype[‘type’],
‘post_title’ => sanitize_file_name($filename),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
];
$attach_id = wp_insert_attachment($attachment, $file, $post_id);
require_once(ABSPATH . ‘wp-admin/includes/image.php’);
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
return $attach_id;
}
Code Explanation
$default_images
: Defines default images for each category using the category slug as the key and the image URL as the value.set_default_thumbnail
: This function runs when a post is saved. If the post has no featured image, it finds the first category with a default image in$default_images
and sets it as the featured image.attach_image_from_url
: This function downloads the image from the URL and attaches it to the post as a media file, making it the featured image.
This code will automatically assign a default featured image for new or edited posts based on their category.
Note: Make sure to test this code in a development environment before implementing it on your live site, and replace the example URLs with your actual default image URLs.
Leave A Comment