Archived topic
adding widget area to 3rd paragraph in the single page
51 replies · Started by Patricia on November 18, 2022
Hi my friends hope all is well!
I would like to add an widget area to the content in the sinle page,
like in the example in this link when there is the 'trending' box, on the third paragraph
can you please help? thanks
Hi there,
I don't believe it's possible using a widget but you can try using a hook:
https://generatepress.com/forums/topic/element-after-first-paragraph/#post-1937353
Hi Leo that might work.I should substitute after_first_paragraph to after_third_paragraph? thanks
and can I add the PHP snippet to the hook and then select it to be displayed after_third_paragraph? is that? thank you
but I dont understand where will add the css code?
ah ok... I can paste the PHP snippet on the settings.php and then it will generate a hook after the paragraph I want. I think it is that :) will test now!
It is working really well thank you!
what if I want to do it more than once? For example
after the 3rd paragraph, then after the 10th and then after the 13th and so on? thanks
You can modify the code for the 10th and 13th, so there'll be 3 code snippets.
I thought that would be too long will it affect page speed?
well I will test and see what happens thank you so much! have a lovelly weekend
Hi guys still with this code, I am trying to add a related post with thumbnail on the left and title on the right. is that possible to achieve this and add it to this hook? thank you!
HI guys it is solved. :) found how to use it. thanks! have a lovely weekend
Hello guys, still hassling with this issue here..
I noticed that on generateblock (using pro) it is possible to use a custom hook. but any time i add this custom hook, the site gets down. It only works if I create a normal hook (not from generateblocks). but does not allow me to do what I want which is: only one featured post on the 3rd paragraph (ideally a related post).
howeer when I try the normal hook, I am using this code to display the 3rd paragraph instead of the 1st. But it only show the text on the frist paragraph.
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_featured_image', 20 );
function insert_featured_image( $content ) {
global $post;
$inserted_hook = do_shortcode('[portable_hook hook_name="after_third_paragraph"]');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 3, $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 + 3 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
My question is, how can I add one featured post (related post) with thumbnail on the left and excerpt on the right, after the 3rd paragraph of each post? Thanks
here is an example of what I want, she puts it in the 4th paragraph. thanks
