- This topic has 7 replies, 3 voices, and was last updated 1 year, 4 months ago by Fernando.
-
AuthorPosts
-
December 4, 2022 at 11:42 pm #2449812yoon
Hello, I am a person using generatepress.
I want to insert an AdSense ad every 3 p tags within the current content.
And I want to limit the total number of ads.
For example, if you limit the number of ads in the content to 7, an ad will be inserted every 3 p tags.
And even if the content is long, if up to 7 ads are inserted, no more ads are inserted.Advertisement 1
<p></p>
<p></p>
<p></p>
Advertisement 2
<p></p>
<p></p>
<p></p>
Advertisement 3
<p></p>
<p></p>
<p></p>
advertisement 4
<p></p>
<p></p>
<p></p>
Advertisement 5
<p></p>
<p></p>
<p></p>
advertisement 6
<p></p>
<p></p>
<p></p>
Advertisement 7
<p></p>
<p></p>
<p></p>
NO ads (because there are already 7 of them)
<p></p>
<p></p>
<p></p>
NO ads (because there are already 7 of them)
<p></p>
<p></p>
<p></p>
NO ads (because there are already 7 of them)And the important thing here is that I want all the ad units to be different. I want each inserted ad unit to be different, rather than one ad unit.
May I know what to do?…
December 5, 2022 at 12:11 am #2449825Fernando Customer SupportHi Yoon,
You can try using a plugin like Ad Inserter. Reference: https://wordpress.org/plugins/ad-inserter/
You can also try doing it through a Hook Element. See David’s two responses here for an example:
https://generatepress.com/forums/topic/adding-widget-area-to-3rd-paragraph-in-the-single-page/page/3/#post-2428638
https://generatepress.com/forums/topic/adding-widget-area-to-3rd-paragraph-in-the-single-page/page/3/#post-2428930With this, you can create custom hooks you can use in a Hook Element to hook an Advertisement.
December 5, 2022 at 5:13 am #2450143yoonIn the second method, there is no syntax that allows only up to 7 advertisements in the content.
I am wondering how to do this part.December 5, 2022 at 8:05 am #2450445DavidStaffCustomer SupportDecember 5, 2022 at 8:10 pm #2451046yoonI want to put ads in the code from the hook rather than a plugin. Can you help me?
December 5, 2022 at 8:26 pm #2451057Fernando Customer SupportAs per David, the best route would be to use the Ad Inserter plugin.
If you want to insert ads through code, you can follow the instructions here: https://generatepress.com/forums/topic/adding-widget-area-to-3rd-paragraph-in-the-single-page/page/3/#post-2428638
Then, add this:
add_filter( 'the_content', 'insert_element_sixth', 25 ); function insert_element_sixth( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_sixth_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 6, $content ); } return $content; }
You, just need to copy and paste this 5 more times if you want 7 hooks.
Just replace the function name every time: insert_element_sixth
Also, replace the value
6
with the number of paragraphs you prefer before adding the Ad.25
is the hook priority, so increase its value every time you copy and paste it.Lastly, replace the hook name as well –
after_sixth_paragraph
For instance:
add_filter( 'the_content', 'insert_element_ninth', 30 ); function insert_element_ninth( $content ) { global $post; $inserted_hook = do_shortcode('[portable_hook hook_name="after_ninth_paragraph"]'); if ( is_single() && ! is_admin() ) { return prefix_insert_after_paragraph( $inserted_hook, 9, $content ); } return $content; }
So with these two, you’ll have these two custom hooks:
after_sixth_paragraph
after_ninth_paragraph
Take note however that we primarily advice you to use Ad inserter.
December 5, 2022 at 8:53 pm #2451064yoonthank you I solved it. But there is one problem.
At the end of the content, there is a copyright and author email using the p tag.
This part is also recognized as a paragraph, and there are cases where advertisements are inserted between the e-mail and the copyright.
So I don’t want ads to be inserted in the last two lines.
Or, at the end of the content, I want to separate the e-mail and copyright sections and add them at the bottom. This seems to be more convenient in some ways, but what should I do?
December 5, 2022 at 9:16 pm #2451079Fernando Customer SupportI think it’s better if you add those two out of the content, or change their tags to Heading tags for instance.
-
AuthorPosts
- You must be logged in to reply to this topic.