If there is a CUSTOM_FIELD_1 in post use image in archive, if not use POST_THUMB
-
Hi there, any help would be appreciated, I’m totally stuck :/
I’m working on a site which has in some of the posts, featured images which have been added to the posts via custom fields, for use in archives / category pages. There are many of these posts, far too many to change by hand.
The rest of the posts have featured images pulled in via the newer ‘featured image’ link in the post.
In the archive / category pages, I need to tell it to look for one of the custom fields ‘custom_field_name_1’ and if that’s not got an image, look for ‘custom_field_name_2’ and if that’s not got an image, pull in the post thumbnail.
And if possible, if there is no thumbnail, use a default image.
Can anyone help?
This seems to pull in the first custom_field_name_1:
CODE 1:<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php $values = get_post_custom_values("custom_field_name_1"); echo $values[0]; ?>" alt="Featured Image" /> </a>
I also found this piece of code which I’ve been trying to play around with, with no success:
<?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 } ?>
The above code came from here: http://www.wpexplorer.com/custom-field-image-wordpress/
What would I need to do to it to make that work?This is what I came up with but it doesn’t cut it:
<?php //if custom field isn't blank if ($alternative_post_image !== 'custom_field_1' ) { ?> <img src="<?php echo $alternative_post_image; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /> <?php } else { ?> <?php //if custom field isn't blank if ($alternative_post_image !== 'custom_field_2' ) { ?> <img src="<?php echo $alternative_post_image; ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /> <?php } else { ?> <?php the_post_thumbnail(); ?></a>
- The topic ‘If there is a CUSTOM_FIELD_1 in post use image in archive, if not use POST_THUMB’ is closed to new replies.