Use A Custom Field Instead of Featured Image In WordPress

Recently I’ve been taking full advantage of the WordPress Featured Images ( or Post Thumbnails as some people call them) for the image slider used in my Free themes. However, some people have been asking me if it was possible to have a separate image for the slider then their post thumbnail because the image slider tends to be so much larger, so I figured I would write up a quick tutorial showing you how to add a custom field to your theme so you can define a separate image for the slider with the “featured image” set as the backup.

Step 1:  Create The Custom Field

The first step is creating the new Custom Field (see the codex if you are unfamiliar with custom fields). Insert the following at the start of the loop which would be right AFTER the “while (have_posts)” or “foreach($myposts as $post” statement. Change where it says “Alternative Image” to whatever makes sense – such as slider image, author image…etc.

//get custom field titled "Post Image"
$alternative_post_image = get_post_meta($post->ID, 'Alternative Image', $single = true);

Step 2: Show New Custom Field Instead Of Featured Image

Now you can pass a quick test to see if the custom field exists and if so show the URL defined in the custom field otherwise show the post thumbnail. Insert the following code where you want to show your image.

<?php
//if custom field isn't blank
if ($alternative_post_image !== '' ) { ?>
<img src="<?php echo $alternative_post_image; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
<?php } else { ?>
<?php
//show featured image
the_post_thumbnail();
?>
<?php } ?>

Step 3: Use The Custom Field In Your Posts

Now you can use your custom field form in the post editor to define the link to your alternative image. Enter the name of your custom field (in this example it is “Alternative Image”) in the “Name” field and then the URL to your image in the “Value” field. Then click on “Add Custom Field”.

Add WordPress Custom Field

If you aren’t sure how to access your Custom Field form and enter your data, please checkout this video: Add Custom Field In WordPress

 

AJ Clarke
Post Author: AJ Clarke

Hey. My name is AJ and I am the boss around here. I own and operate WPExplorer. A website dedicated to everything I love about WordPress.

Disclosure: This page contains external affiliate links that may result in us receiving a comission if you choose to purchase said product. The opinions on this page are our own. We do not receive payment for positive reviews.
Got something to say? Join the discussion.
  1. HI there, I do not seem to have "while (have_posts)" in my theme, is there an updated code, if so I would greatly appreciate it. I am stuck with a theme, that is using featured images for widgets, and I have auto generated custom field urls with no extensions, thank you
    • AJ Clarke | WPExplorer says:
      AJ Clarke | WPExplorer
      Not really understanding the question...Maybe best if you read up on the Codex: http://codex.wordpress.org/Custom_Fields
      Admin

Leave a Reply