- This topic has 5 replies, 3 voices, and was last updated 1 year, 1 month ago by David.
September 8, 2020 at 1:58 pm #1435831Gareth
I appreciate the title might be confusing. I will try to explain.
I am trying to build a header element that pulls a unique image for each post but ISN’T the featured image.
An example can be found on (not Generatepress) site here: https://independentmusicpodcast.net/ where you can see that the images in the header are different to the featured images for the same posts further down the page.
This is achieved using a custom field for each post, that then points to an image file (in this case the custom field is ‘secondline_themes_header_image’).
Back in Elements, I have three options for background image:
– No background image
– Featured image
– Custom image
But the custom image would be the same across all posts, whereas I want to make it unique for each post.
How do I go about doing that?
Thanks in advance – the theme is amazing, making my new site lightning fast without a load of heavyweight plug-insSeptember 8, 2020 at 2:39 pm #1435847ElvinStaffCustomer Support
You can try this PHP code:
<?php if ( get_post_meta( get_the_ID(), 'secondline_themes_header_image', true ) ) : ?> <img class="post-unique-img" src="<?php echo esc_url( get_post_meta( get_the_ID(), 'secondline_themes_header_image', true ) ); ?>" alt="<?php the_title_attribute(); ?>" /> <?php endif; ?>
This will generate an image depending on the value of your “secondline_themes_header_image” custom field. Just make sure to add a valid URL value and make sure that this URL value is actually unique from other posts.
Let us know if it works for you.
A wise man once said:
"Have you cleared your cache?"September 9, 2020 at 12:51 am #1436162Gareth
Thanks Elvin, where am I putting that? In the Element code or the post? I’ve tried both and neither seem to work.September 9, 2020 at 6:36 am #1436501DavidStaffCustomer Support
I would suggest the PHP Snippet provided here:
This will allow you to add your Custom Field background, if that is left empty then it will use the post featured image, and if that is empty it will use the custom background image you set in the element.
In the code you will see this line:
$background_image = get_post_meta( get_the_ID(), 'wpcf-common_featured_media', true );
wpcf-common_featured_mediais the Custom field slug – change that to your custom field.
The code is PHP and this article explains how to add:September 12, 2020 at 4:17 am #1440336Gareth
That works! Thanks David 🙂September 12, 2020 at 6:28 am #1440451
- You must be logged in to reply to this topic.