[Support request] I am curious about how to insert adsense ads.

Home Forums Support [Support request] I am curious about how to insert adsense ads.

Home Forums Support I am curious about how to insert adsense ads.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2449812
    yoon

    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?…

    #2449825
    Fernando
    Customer Support

    Hi 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-2428930

    With this, you can create custom hooks you can use in a Hook Element to hook an Advertisement.

    #2450143
    yoon

    In 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.

    #2450445
    David
    Staff
    Customer Support

    Hi there,

    use the Ad Inserter plugin:

    https://en-gb.wordpress.org/plugins/ad-inserter/

    #2451046
    yoon

    I want to put ads in the code from the hook rather than a plugin. Can you help me?

    #2451057
    Fernando
    Customer Support

    As 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.

    #2451064
    yoon

    thank 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?

    #2451079
    Fernando
    Customer Support

    I think it’s better if you add those two out of the content, or change their tags to Heading tags for instance.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.