Archived topic
how can i make my featured image the same location like this?
9 replies · Started by eran on June 27, 2022
Hi,
how can i make my featured image the same location like this one?
https://woorkup.com/add-php-to-wordpress/
Hi there,
This topic should help:
https://generatepress.com/forums/topic/element-after-first-paragraph/
You can create a custom hook after the first paragraph, then use a block element (element type: hook) to add the feature image.
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?
You can use a block element, add an image block of GenerateBlocks, enable its dynamic data, set it to featured image.
https://docs.generateblocks.com/article/image-overview/
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!
To disable the Featured Image, you can use a Layout Element: https://docs.generatepress.com/article/layout-element-overview/#disable-element
Hope this clarifies!