- This topic has 6 replies, 4 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
November 28, 2022 at 7:48 pm #2440193
Heath
Just wondering if there is a way to manually add the adsense ad every 3 or 4 paragraphs?
November 28, 2022 at 8:03 pm #2440210Fernando Customer Support
Hi Heath,
A popularly used plugin for such a feature is “Ad Inserter”. Reference: https://wordpress.org/plugins/ad-inserter/
Another way is through custom code. Try adding this snippet:
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_every_third_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_every_third_paragraph( $inserted_hook, 3, $content ); } return $content; } function prefix_insert_after_every_third_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 ( $index % $paragraph_id == 0 ) { $paragraphs[$index] .= $insertion; } } return implode( '', $paragraphs ); }Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
With this, you can add your Advertisement code through a Hook Element, and hook it to
Custom Hook - after_every_third_paragraph.Example: https://share.getcloudapp.com/12uR6w2O
Hook Element reference: https://docs.generatepress.com/article/hooks-element-overview/
November 29, 2022 at 2:48 pm #2441903Heath
Thanks! For the PHP do I need a plugin to update that?
November 29, 2022 at 4:52 pm #2441993Ying
StaffCustomer SupportSome methods introduced here: https://docs.generatepress.com/article/adding-php/
Quick answer, Code Snippet plugin would do.
December 16, 2022 at 10:28 am #2464460Heath
What about every 4 paragraphs instead of every 3? I tried to update the snoppet but got an error so I didn’t do something right.
December 16, 2022 at 6:06 pm #2464713Heath
Any suggestions?
December 17, 2022 at 5:03 am #2464978David
StaffCustomer SupportHi there,
on this line:
return prefix_insert_after_every_third_paragraph( $inserted_hook, 3, $content );Change the
3to4so it becomes:return prefix_insert_after_every_third_paragraph( $inserted_hook, 4, $content ); -
AuthorPosts
- You must be logged in to reply to this topic.