WordPress isn’t going out of style anytime soon, so you might as well jump right into it and get fancy with your featured images. Featured images (aka post thumbnails) are images that are linked to pages, posts, or custom post types. These images not only provide visual appeal, they also help with SEO!
Setting a featured image can easily be done through the WordPress dashboard. But it can also be done programmatically, which we’re big fans of.
Benefits of Programmatically Setting Featured Images
You can automate the task, setting the featured image based on a post category or tag. You can also incorporate conditional logic into your code – for instance, you might want a set of fun light-hearted images on the weekends, and something more businessy-y during weekdays. The possibilities are endless really.
From a branding perspective, setting the featured image programmatically can help minimize any human error and ensure that your site keeps a consistent brand. Plus, a lot of time is saved by doing things the programmatic way. Seriously, who wants to spend time setting images one by one?
Implementation: Setting Featured Images Based on Tags
In this snippet, we’ll set featured images based on tags. We’ll also pull from a pre-selected list of images.
// Define an array of image URLs
$image_urls = array(
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg'
);
// Function to set featured image based on tags
function set_featured_image_based_on_tags() {
// Get posts based on specific tags
$tag_args = array(
'tag' => 'your_tag_slug', // Replace 'your_tag_slug' with the slug of your desired tag
'posts_per_page' => -1 // Get all posts with the specified tag
);
$tag_query = new WP_Query( $tag_args );
// Loop through each post
if ( $tag_query->have_posts() ) {
while ( $tag_query->have_posts() ) {
$tag_query->the_post();
// Choose a random image URL from the array
$random_image_url = $image_urls[ array_rand( $image_urls ) ];
// Set the random image URL as the featured image
set_post_thumbnail( get_the_ID(), media_sideload_image( $random_image_url, get_the_ID() ) );
}
}
// Reset post data
wp_reset_postdata();
}
// Hook the function to run when WordPress initializes
add_action( 'init', 'set_featured_image_based_on_tags' );
In this snippet we’re doing the following:
- First, we define an array of preselected images.
- Inside the set_featured_image_based_on_tags() function, we create an instance of WP_Query to fetch posts based on the tag we defined.
- If $tag_query->have_posts() returns posts, we enter the loop.
- Once in the loop, $tag_query->the_post() is called to set up the post data.
- A random image is chosen from our predefined image set: array_rand($image_urls).
- Once the random image is chosen, we use that to set the feature image:
set_post_thumbnail(). - When we’re out of the loop, we reset our post data and make sure our function is hooked to the “init” action using add_action().
Customization
This is pretty straightforward code, but you can easily see the possibilities here. Here we defined an array filled with a few images, but you can extend this to retrieve an image URL dynamically from an external API. You can also further customize all of your images by adding your brand’s icon or other photo enhancements.
This simple bit of code has the power to bring your WordPress site to the next level. Not only does it lighten your workload, it also ensures consistency across your brand.