Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

featured image in single post

  • Hi there,

    I would like to insert the featured image in each single post.
    It’s better to insert that below a certain title.

    I tried to play with options in theme “customization”, can’t find the function for that.
    any advise

  • Hi there,

    You can use the Element module to add the featured image. However, if you want to place it within the content area, there’s a bit of a workaround because there is no hook available to place an element anywhere inside the content area.

    First, you need to be certain about where to place the featured image, for example, after the first heading, second heading, or elsewhere. Once you decide on that, I can provide a snippet to help you achieve your desired placement.

  • thanks.
    I would like it to be after a certain heading.
    It is stated in the private information section.

  • Based on the post, it will be under the third H2 heading. Is this the same for other posts as well?

  • yes. I assume all the posts have their own featured image under the third H2 heading.

  • Okay, so in that case you’ll need to do the followings:

    Add this snippet:

    add_filter('the_content', function( $content ) {
        preg_match_all('/<\/h2>/', $content, $matches, PREG_OFFSET_CAPTURE);
        $closing_tags = $matches[0];
        
        if (count($closing_tags) < 3) return $content;
        
        $insert_position = $closing_tags[2][1] + strlen('</h2>'); // Position after the third closing </h2> tag
        $content = substr_replace($content, do_shortcode('[portable_hook hook_name="after_h2_heading"]'), $insert_position, 0);
        
        return $content;
    });
    
    add_shortcode('portable_hook', function($atts){
    	ob_start();
            $atts = shortcode_atts( array(
                'hook_name' => 'no foo'
            ), $atts, 'portable_hook' );
    		do_action($atts['hook_name']);
    	return ob_get_clean();
    });

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    Next, create a new Block Element and add a GB Image block. Insert any image, then select the image block.

    Under Block settings, activate the Dynamic Data option. Set DATA SOURCE to Current post and IMAGE SOURCE to Featured image.

    Then go to Element settings and set ELEMENT TYPE to Hook, HOOK NAME to Custom Hook, and CUSTOM HOOK NAME to after_h2_heading.

    Finally, under Location settings, choose Post > All Posts.

    Let me know if you have any questions.

  • thanks very much.
    follow your instruction, it works well.

  • You’re welcome, great to hear that.

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