Site logo

Archived topic

Is there a Template Tag for Featured Images?

9 replies · Started by Bill Forsyth III on February 1, 2021

Viewing posts 1–10 of 10

Hello,
I'm experimenting with Header Elements, and I see that there are some template tags like {{post_date}} and {{post_terms.taxonomy}}.

I'm looking to be able to use the featured image, and I got crazy and tried {{featured_image}} to no avail.

Is there a way to use featured image in the header elements, and I guess more useful would be a list of all of the available template tags ... or are the 5 listed the only ones that exist?

Thanks!
Bill

Hi there,

There's no template tag for featured image as of this moment but here's a suggestion:

We can make a shortcode for the purpose of displaying the featured image.

Try adding this PHP snippet:

add_shortcode( 'featured_image', function( ) {
ob_start();
global $post;
$featured_img_url = get_the_post_thumbnail_url($post->id);
echo '<img class="page-hero-featured-image" src="'.$featured_img_url.'" alt="'.get_the_title($post->id).'"/>';
return ob_get_clean();
});

You can then use the shortcode [featured_image] to display the current post's featured image with this. This would act like the {{featured_image}} you were aiming for.

Awesome! Thank you! That works. So are there no other template tags? Should we just create a shortcode if we're needing something else than the 5 template tags that are displayed?

What's the template tag to generate post content? Might you know? I tried {{post_content}} but that didn't work. Hahaha...

p.s. - if I create new block element, I see the dynamic tag but I don't want to use this block.

Hi there,

I don't think there's a template tag for post content.

Any specific reason you don't want to use dynamic content block?

I want to created a no sidebar, merged header, full screen landing page with a parallax background image. I dabbled with the (new) Block element type but I believe it was to complex of a layout (but simple with old GP).

Maybe I'll try it again... but I felt pretty sure this was not possible with my existing site.

It does seem odd, though, that there is no template tag for post content...seems like the code is already baked-into GP but not as easily accessible. To bad...

If the main concern is having to display the_content within the header, we can make a quick shortcode for that.

Add this PHP snippet:

add_shortcode( 'post_content', function() {
    ob_start();
    // Start your PHP below
  
    the_content();
  
    // End your PHP above
    return ob_get_clean();
} );

Here's how to add PHP snippets - https://docs.generatepress.com/article/adding-php/

With this, you can use the shortcode [post_content] to show the content of the post on the header. (or anywhere the shortcode is placed)

Bravo!

@Webmaster,

Have you fully sorted it out? Let us know if you need further help. :D

This archived topic is closed to new replies.