Easy Code for Featured Images in WordPress®

A white, rounded square logo with an abstract S-shaped design on a gradient blue and purple background.

Below is a really easy code sample for utilizing featured images in WordPress®.

Easy Code for Featured Images in WordPress: A PHP code snippet for determining and extracting the URL of a post's featured image or setting a default image if none exists.

To implement this you would basically just copy and paste the code snippet (found in text format below) where you want to utilize the featured image section in WordPress® and the $featured_image variable would return the URL of the featured image.  The ‘else’ part of the statement sets a fallback (default) photo if a featured image is not chosen for a specific page.

Simple, but nice.

 

FOR COPYING & PASTING PURPOSES:

<?php

if(has_post_thumbnail()) { // check if the post has a Featured Image.

$photo = get_the_post_thumbnail();

$featured_image = explode(‘”‘, $photo);

$featured_image = $featured_image[‘1’];

} else {

$featured_image = ‘/wp-content/themes/paperstreet/images/default.jpg’;

}

?>

Blog Decorative Background

Join the Sales and Marketing News, receive our last insights, tips and best practices.

Our 7 Guarantees

Keeping 2,000+ Clients Happy Since 2001.

1
You Will Love Your Design We design to please you and your clients
2
Same-Day Support 24-hour turnaround edits during business hours
3
Free Education We provide knowledge to help you expand
4
No Hidden Charges We quote flat-rate projects
5
Own Your Site No strings attached
6
We Create Results SEO, PPC, content + design = clients
7
We Make Life Easier One agency for web, branding and marketing