Site logo

Archived topic

how can i make my featured image the same location like this?

9 replies · Started by eran on June 27, 2022

Viewing posts 1–10 of 10

What do you mean create custom hook after first paragraph?
cant I use only the block element?

There isn't such a hook that locates below the first paragraph of the post in the theme.

So you'll have to create a custom hook for this specific location:
https://www.screencast.com/t/zRRpuS2N7630

Hope I made that clear.

I understand but how can i put the featured image with a hook block? i dont get it.
i used the php code in the child theme what should i do next?

Didn't work

OK now it worked.
how can i deisable all post featured images? i want to use the featured image after the first paragraph

Hi Eran,

Try using this snippet instead:

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();
});

add_filter( 'the_content', 'insert_my_element', 20 );
function insert_my_element( $content ) {
    global $post;
    $inserted_hook = do_shortcode('[portable_hook hook_name="after_first_paragraph"]');
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $inserted_hook, 1, $content );
    }
    return $content;
}

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
  $closing_p = '</p>';
  $paragraphs = explode( $closing_p, $content );
  foreach ($paragraphs as $index => $paragraph) {
    if ( trim( $paragraph ) ) {
     $paragraphs[$index] .= $closing_p;
    }
    if ( $paragraph_id == $index + 1 ) {
     $paragraphs[$index] .= $insertion;
    }
  }
  return implode( '', $paragraphs );
}

add_filter( 'generate_hooks_list', function($hooks){
    $hooks['content']['hooks'][] = 'after_first_paragraph';
    return $hooks;
}, 99,1 ); 

This will add a hook called after_first_paragraph to the available hooks as such: https://share.getcloudapp.com/NQulYbWj

Then, setup the Image Block as such: https://share.getcloudapp.com/KoujJy9v

Hope this helps!

This archived topic is closed to new replies.