0

Each post of my blog has a Custom Field which is the URL of a photo on Flickr. How can I use it as the Featured Image?

Please note I'm asking this question because I'm migrating to a theme that makes use of the Featured Image to display thumbnails on the blog’s home page, but I would avoid enter them all by hand for all the 300+ old posts.

EDIT:

  • I'd prefer to avoid to change the theme's code, to avoid complications during future updates. But I can use child themes.
  • The theme I'm using uses the get_post_thumbnail_id() function to obtain the featured image attached to the post:

    <?php if ( has_post_thumbnail() ) :
    $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
    ?>
    <div class="post-thumbnail-wrapper">
        <a class="swipebox" href="<?php echo $full_image_url[0]; ?>" title="<?php printf( esc_attr__('Permalink to image of %s', 'envirra'), the_title_attribute('echo=0') ); ?>" rel="bookmark">
            <?php the_post_thumbnail( 'vw_large' ); ?>
        </a>
    </div>
    
  • Please post the actual code from your Theme, that is used to generate the image. Otherwise, there's no way to provide a specific answer. get_post_thumbnail_id() merely returns a Post ID for an attachment post-type. – Chip Bennett Jan 15 '14 at 17:24
  • Based on the posted code, you're going to have to edit your template to accomplish what you're after. – Chip Bennett Jan 15 '14 at 17:52
2

Editing the Template

The easiest solution would be to use a conditional to display the featured image:

if has custom field use it, else use Featured Image

You can get the custom field using get_post_meta(), which will return an empty string if the specified key isn't set:

$custom_url = get_post_meta( get_the_ID(), 'old_featured_image_custom_field', true );

You can get the Featured Image using the_post_thumbnail(), or check for its existence via has_post_thumbnail().

Using these, you can set up a conditional output, e.g. like so:

<?php
$custom_url = get_post_meta( get_the_ID(), 'old_featured_image_custom_field', true );

if ( '' != $custom_url ) {
    ?>
    <img src="<?php echo $custom_url; ?>" />
    <?php
} else if ( has_post_thumbnail() ) {
    the_post_thumbnail();
}

Using a Filter

If you can't or don't want to edit the template, you're in luck: the_post_thumbnail() calls get_the_post_thumbnail(), which includes a filter, post_thumbnail_html:

return apply_filters( 'post_thumbnail_html', $html, $post_id, $post_thumbnail_id, $size, $attr );

So, just write a filter callback using the same method:

function wpse129849_filter_the_post_thumbnail( $html, $post_id ) {
    // Try to find custom field value
    $custom_url = get_post_meta( $post_id, 'old_featured_image_custom_field', true );
    // If it has a value, return it
    if ( '' != $custom_url ) {
        return '<img src="' . $custom_url . '" />';
    } 
    // otherwise, just return the original post thumbnail html
    else {
        return $html;
    }
}
add_filter( 'post_thumbnail_html', 'wpse129849_filter_the_post_thumbnail', 10, 2 );
  • It's a nice idea, but it would require a change to the theme's code. I'd prefer to avoid doing so, to avoid complications during future updates... – User Jan 15 '14 at 14:03
  • Please see updated answer. You can use a filter as well. – Chip Bennett Jan 15 '14 at 14:16
  • Thanks! But from what I see my theme uses the get_post_thumbnail_id(), not the get_the_post_thumbnail(). Any suggestions? – User Jan 15 '14 at 14:47
  • Before I edit my answer again, please edit your question to include all relevant information, including all relevant code in context, your no-template-editing limitation, and anything else that might restrict solutions. – Chip Bennett Jan 15 '14 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.