Archived topic
adding widget area to 3rd paragraph in the single page
51 replies · Started by Patricia on November 18, 2022
The PHP code should be not added using one of these methods only:
Adding PHP: https://docs.generatepress.com/article/adding-php/
It should not be added through hooks or theme files.
And CSS can be added using one of these methods:
Adding CSS: https://docs.generatepress.com/article/adding-css/
Please make sure the code is added correctly first.
I am creating it on generateblocks pro and then I set the custom hook to the third paragraph but every time i do it, the site goes down. any thoughts on it? thanks
I thought in a previous reply you've mentioned that it worked?
What changed after?
1st - I added the php code to the functions and created a custom element hook placement under the 3rd paragraph.
2nd - Then I created a hook and set it to be displayed on a custom hook. initially it worked fine, under the 3rd paragraph with plain text. But I am looking to display one blog post suggestion under the third paragraph () Initially when I said it worked, it actually worked with plain text, but it was not what I was trying to achieve.
So, After experimenting with it a little ,I managed to create the blog suggestion box and it looked exactly as I wanted by using genarateblocks, grid, and custom loop.
In this block a set the grid as I wanted, got the look and configuration to what I wanted. So far so good. Next step would be to place it on the custom hook to place it under 3rd parapragh but any time I do it the site goes down.
Does that make more sense now? many thanks
Hi there,
If you set the Query Loop to display just the Title, does it work ok ?
HI David it is working like you suggested. I have another issue tho; despite adapting the php code to show after the 3rd paragraph, it is being displayed only after the 1st paragraph....
here is the code:
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 );
}
OK, so the excerpt won't work, as its a trimmed version of the_content which means the excerpt is also being filtered by your code, which is creating an infinite loop.
Are you ok with just the title? ( i am kinda hoping for the answer to be yes lol )
Where can i see the post that has it inserted after the 1st paragraph ?
HI David. Yes it is ok if we leave only the title. But would be really nice if I am able to inject these boxes in between paragraphs, not only after the 3rd but after other paragraphs in the content. Here is a link of the staging site, I am sharing in the private box please let me know if you are able to open it. Many thanks
Couple of questions:
1. What method are you using to insert the adverts?
2. And if we can inject after multiple paragraphs, will the each injected content be different to the other content ?
1. What method are you using to insert the adverts? the adverts are displayed by mediavine, they take care of it; but I want to add normal content boxes or related articles. do you know what I mean?
here is the link on how it is now: https://themakeuporg.bigscoots-staging.com/simple-skincare-routine/
it is being displayed after the first paragraph, but I'd like it to be after the 3rd.
2. And if we can inject after multiple paragraphs, will the each injected content be different to the other content ?
yes it would be different for example:
- after the 3rd paragraph: 1 recommended post
- after the 7th paragraph: 1 call to action box to subscribe
- after the 11th paragraph: 1 call to action box to follow on social media
Hope that makes sense :)
Many thanks and talk to you soon!
OK :)
Lets see if we can fix the positioning first:
What happens if you increase the 3 in the PHP Snippet to a greater number eg . 5 does the content move ?
Hi David!
Unfortunatelly it doesnt move :/
it is intriguing because the first time I tested this code it worked fine under de 3rd paragraph...I havent installed anything after that..
When you tested the code - was that on a live site with the adverts ?
was that on a live site with the adverts ? yes I was testing in live site then after it shut down the site I decided to test on staging
