Archived topic
adding widget area to 3rd paragraph in the single page
51 replies · Started by Patricia on November 18, 2022
Can you 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_element', 20 );
function insert_element( $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 + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
Hello David!
Ok it works :) but when I try to copy and paste the code to create the next custom hook I have this erros message: https://ibb.co/VVfd7Hm
thank you!
Ok, you can't duplicate the code.
If you put it back to the one code where it was working ( and double check that :) )
Now try adding this: - i set it to the 7 seventh paragraph. That will return a hook titled: after_seventh_paragraph
add_filter( 'the_content', 'insert_element_seventh', 25 );
function insert_element_seventh( $content ) {
global $post;
$inserted_hook = do_shortcode('[portable_hook hook_name="after_seventh_paragraph"]');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 7, $content );
}
return $content;
}
If that works then add another code for the after_eleventh_paragraph
add_filter( 'the_content', 'insert_element_eleventh', 30 );
function insert_element_eleventh( $content ) {
global $post;
$inserted_hook = do_shortcode('[portable_hook hook_name="after_eleventh_paragraph"]');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 11, $content );
}
return $content;
}
Hi david, yes the code was working, the only problem is that couldnt duplicate. I will test the new codes and will confirm with you! thank you so much! :) and talk soon!
Awesome - let us know :)
Hi David! I am trying the additional codes,
Here is the situation:
1. I created a hook with testimonials with the intention to place it under the 7th paragraph
2. I created another hook with a table to be placed under the 11th paragraph
however, all placements are showing(repeating) the related post hook. and they appear one after another instead of under paragraphs 7 and 11. you can see it on the link I am sharing! thank you!
Hmm.... peculiar and we're to begin with debugging that.
First thing i noticed, in the Query Loop block, the Headline Block with the Date Meta is set to a p - could you change that to DIV ? This will avoid it getting counted in the content.
In the meantime ill try and get my head around what is happening here :)
Hi David!
Where do you mean the query look block? This is the first block in which I added the 3 suggested articles, correct? If this is the case, I deleted it and added an info box for testing purposes, and the problem persists. the link is in private area!
thank you!
Sorry for the delay, i just booked myself in to "how not to be an idiot and not check the function names" class lol
So the issue is with the 2 additional snippets i provided here:
Both i have now fixed, can you give them a try ?
Sure will give it a try! No worries! You are doing excellent work, David!! :) will try and let you know! Thanks
talk soon!
Hi David, now, it only displays thee box after the 3rd paragraph.
maybe I am not pasting it correctly? I tried like this
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_element_seventh', 25 );
function insert_element_seventh( $content ) {
global $post;
$inserted_hook = do_shortcode('[portable_hook hook_name="after_seventh_paragraph"]');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 7, $content );
}
return $content;
}
and have also tried just adding this code alone as well
add_filter( 'the_content', 'insert_element_seventh', 25 );
function insert_element_seventh( $content ) {
global $post;
$inserted_hook = do_shortcode('[portable_hook hook_name="after_seventh_paragraph"]');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 7, $content );
}
return $content;
}
in any case it shows only after the 3rd paragraph!
thank you so so much!
OK, remove any snippets you have so far and add:
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_element', 20 );
function insert_element( $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;
}
add_filter( 'the_content', 'insert_element_seventh', 25 );
function insert_element_seventh( $content ) {
global $post;
$inserted_hook = do_shortcode('[portable_hook hook_name="after_seventh_paragraph"]');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 7, $content );
}
return $content;
}
add_filter( 'the_content', 'insert_element_eleventh', 30 );
function insert_element_eleventh( $content ) {
global $post;
$inserted_hook = do_shortcode('[portable_hook hook_name="after_eleventh_paragraph"]');
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $inserted_hook, 11, $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 );
}
Then there should be three hooks for you to use:
after_third_paragraph
after_seventh_paragraph
after_eleventh_paragraph
Oh No! It is not working :/ not even like this. shows only after_third_paragraph
Do you have any cache plugins ? If so can you try clearing those ?
yes cleared caching! :)
