[Resolved] Is there a Template Tag for Featured Images?

Home Forums Support [Resolved] Is there a Template Tag for Featured Images?

Home Forums Support Is there a Template Tag for Featured Images?

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1642351
    Website Psychiatrist

    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

    #1642656
    Elvin
    Staff
    Customer Support

    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.

    A wise man once said:
    "Have you cleared your cache?"

    #1643710
    Website Psychiatrist

    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?

    #1643715
    Elvin
    Staff
    Customer Support

    You can check this brief documentation for all the other template tags:
    https://docs.generatepress.com/article/header-element-template-tags/

    For the other ones, yes, you’ll have to create your own shortcode.

    A wise man once said:
    "Have you cleared your cache?"

    #1897791
    Webmaster

    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.

    #1897855
    Ying
    Staff
    Customer Support

    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?

    #1898260
    Webmaster

    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…

    #1898264
    Elvin
    Staff
    Customer Support

    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)

    A wise man once said:
    "Have you cleared your cache?"

    #1899489
    Webmaster

    Bravo!

    #1899545
    Elvin
    Staff
    Customer Support

    @Webmaster,

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

    A wise man once said:
    "Have you cleared your cache?"

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.