PHP Help Needed to show thumbnail (confusing & a challange!)
-
Hi guys,
I’m migrating a site from an old WordPress installation, back in the days when you had to use a custom field to generate a post image (which now is of course Featured Image).
Here’s what I want to achieve:
Show Featured Image
Otherwise show custom field
Otherwise show default (if not Feature Image and not Custom Field).The code I have for this is:
<?php $field_name="thumbnail"; $field_value = get_post_meta($post->ID, $field_name, true);?> <a href="<?php the_permalink(); ?>"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'thumbnail' ); } else if( isset($field_value) && !empty($field_value) ) { echo '<img src="' . $field_value .'" class="sqthumb" />'; } else { echo '<img src="/wp-content/uploads/news-default1-150x150.jpg" class="sqthumb" />'; } ?> </a>
Just to state, as it’s slightly confusing, my custom field is called thumbnail, as is the size of the featured image I’m calling.
The problem with this code, is that it works fine, except for on posts that don’t have a featured image but has an image embedded into the content. Even if those posts have a custom field associated with them, it refuses to show anything ??
Is there some way I can get it to show the custom field (preferable)? If not, I’ve come across catch_that_image(); which catches the first image in the content and works as a standalone, but doesn’t work in the above markup. Ideally in this instance, I’d like to have:
Show Featured Image
Otherwise show custom field
Otherwise first image of post
Otherwise show defaultI hope this makes sense and someone can help me!
Many thanks!
Matt
- The topic ‘PHP Help Needed to show thumbnail (confusing & a challange!)’ is closed to new replies.