Archived topic
How to create a latest post header image on all the pages
4 replies · Started by Giahung on July 5, 2020
I was wondering how do I create a header image that shows the latest post on all of the pages on my site?
It seems when I try to create a header element, it only creates a header image from that specific post, but I want the image to be the same for all of my pages using the latest post.
Hi there,
Just to confirm, you want your Header Element to show your latest post from the blog on all pages instead of data related to the current page?
If so, you could try using a plugin like WP Show Posts to output a single latest post using a shortcode in the element: https://wpshowposts.com/
Yeah that's exactly what I want. Lemme give it a try and let you know how it goes.
Will you be adding in this feature in the future to generatepress so that we are able to show a featured image using our latest post?
So I just tried adding in the short code from wp show post, but it seems to only a medium sized image as if I was posting a blog post.
Do I get it to show the image like a header image?
What sort of texts should I be putting into the header element?
So you want the featured image from that latest post to display as the background image for the Header Element?
If so, this might help:
add_filter( 'generate_page_hero_background_image_url', function( $image_url, $options ) {
if ( 1234 === $options['element_id'] ) {
$latest_posts = get_posts( 'numberposts=1' );
$latest_id = $latest_posts[0]->ID;
$image_url = get_the_post_thumbnail_url( $latest_id );
}
return $image_url;
}, 50, 2 );
You just need to update 1234 to the ID of the Element you've created.