- This topic has 9 replies, 3 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
November 16, 2022 at 11:29 am #2418594
Andrew
I’d like to hook a block element within the content area so readers see it as they scroll throughout each post. Ideally, I’d be able to position the block a certain distance down the post (i.e., 25% or 50% down the post, or after the first 10 paragraphs). Is this possible? If so, how?
Thank you!
November 16, 2022 at 11:32 am #2418598Leo
StaffCustomer SupportHi Andrew,
Take a look at this solution here:
https://generatepress.com/forums/topic/element-after-first-paragraph/Let me know 🙂
November 16, 2022 at 12:04 pm #2418622Andrew
Awesome! It worked but how can I modify this so the hook show up after the 10th or 20th paragraph?
November 16, 2022 at 5:51 pm #2418863Fernando Customer Support
Hi Andrew,
Yes, it should be possible.
Try changing the value of
1to your preference(ex.10or20) in this line of code:return prefix_insert_after_paragraph( $inserted_hook, 1, $content );.January 31, 2023 at 10:43 am #2515941Andrew
hi Again,
I have this all set up and currently have a block that hooks within the content after 10 paragraphs.
Is there a simple way to add a second hook to the content, after 30 paragraphs for example? Would I need to copy/paste the code here https://generatepress.com/forums/topic/element-after-first-paragraph/ a second time?
To be clear, I want two hooks within content. The one I already have after 10 paragraphs and another one after 30 paragraphs.
Not sure if it matters but I want the same hook to appear twice.
Thank you!
January 31, 2023 at 10:47 am #2515950Leo
StaffCustomer SupportWould I need to copy/paste the code here https://generatepress.com/forums/topic/element-after-first-paragraph/ a second time?
This should be the correct method.
You would need to change the name of the hook (ie.
after_first_paragraph) to a different name though.January 31, 2023 at 6:43 pm #2516471Andrew
Thanks Leo – When I tried to paste the code again I get this error:
Your PHP code changes were rolled back due to an error on line 126 of file wp-content/themes/prudentreviews/functions.php. Please fix and try saving again.
Cannot redeclare insert_featured_image() (previously declared in wp-content/themes/prudentreviews/functions.php:95)
January 31, 2023 at 7:17 pm #2516489Fernando Customer Support
You just need to copy and paste this part:
add_filter( 'the_content', 'insert_featured_image', 20 ); function insert_featured_image( $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 ); }But alter the function name and other stuff. Example:
add_filter( 'the_content', 'insert_featured_image2', 20 ); function insert_featured_image2( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_thirtieth_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 30, $content ); }This hook should now be available with this new code:
after_thirtieth_paragraphFebruary 1, 2023 at 2:58 pm #2517658Andrew
I just tried that and got the error “Your PHP code changes were rolled back due to an error on line 122 of file wp-content/themes/prudentreviews/functions.php. Please fix and try saving again.
syntax error, unexpected end of file”
This is the code I’m using, including the first part which is working fine.
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_first_paragraph”]’); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 10, $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( ‘the_content’, ‘insert_featured_image2’, 20 ); function insert_featured_image2( $content ) { global $post; $inserted_hook = do_shortcode(‘[portable_hook hook_name=”after_fiftieth_paragraph”]’); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 50, $content ); }February 1, 2023 at 5:33 pm #2517767Fernando Customer Support
At first glance at the code you pasted here, I can see that the quotation marks are slanted.
It needs to be replaced with straight quotation marks.
When copying and pasting, somethings, straight quotations are replaced with slanted quotations. This will cause issues.
Please replace all slanted quotation marks with straight ones first. Example – This
‘to this' -
AuthorPosts
- You must be logged in to reply to this topic.