• Resolved shariarkuasa

    (@shariarkuasa)


    Hi,
    I recently installed wp Astra theme which is amp supported and activate the paired mode in AMP. Previously I’ve two Adsense units on my single pages – one below the title and one within content after 2 paragraphs. I added the following code in single-layout.php

    <?php astra_single_header_after(); ?>

    <div class=”entry-content clear” itemprop=”text”>
    <?php if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) : ?><amp-ad
    layout=”fixed-height”
    height=100
    type=”adsense”
    data-ad-client=”ca-pub-5367747358451280″
    data-ad-slot=”1919115743″>
    </amp-ad><?php else : ?><div class=”top-ad”><style>
    .gas_below_title { width: 320px; height: 100px; }
    @media(min-width: 500px) { .gas_below_title { width: 435px; height: 300px; } }
    @media(min-width: 800px) { .gas_below_title { width: 728px; height: 90px; } }
    </style>
    <script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
    <!– GetAndroidStuff Below Title –>
    <ins class=”adsbygoogle gas_below_title”
    style=”display:inline-block”
    data-ad-client=”ca-pub-5367747358451280″
    data-ad-slot=”2618604399″>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script></div><?php endif; ?>
    <?php astra_entry_content_before(); ?>

    Which perfectly work on both regular and amp version. However, for the second ad unit, I used to add the following code in functions.php. Now I can’t figure out how to implement the amp version of adesnes code within the following code in functions.php. Please help within the following code

    //Insert ads after the second paragraph of single post content.

    add_filter( ‘the_content’, ‘prefix_insert_post_ads’ );

    function prefix_insert_post_ads( $content ) {

    $ad_code = ‘<div class=”td-g-rec td-g-rec-id-content_inline”><script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
    <ins class=”adsbygoogle”
    style=”display:block; text-align:center;”
    data-ad-format=”fluid”
    data-ad-layout=”in-article”
    data-ad-client=”ca-pub-5367747358451280″
    data-ad-slot=”5639820018″>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script></div>’;

    if ( is_single() && ! is_admin() ) {
    return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

    return $content;
    }

    // Parent Function that makes the magic happen

    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 );
    }

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Weston Ruter

    (@westonruter)

    So you are trying to figure out how to construct an amp-ad? This would be a better question for the general AMP support forum: https://amp.dev/support/

    You need support from someone who is more familiar with amp-ad.

    Thread Starter shariarkuasa

    (@shariarkuasa)

    Can you help me to alter the code that I can put in the functions.php

    this part

    add_filter( ‘the_content’, ‘prefix_insert_post_ads’ );

    function prefix_insert_post_ads( $content ) {

    $ad_code = ‘<script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
    <ins class=”adsbygoogle”
    style=”display:block; text-align:center;”
    data-ad-format=”fluid”
    data-ad-layout=”in-article”
    data-ad-client=”ca-pub-5367747358451280″
    data-ad-slot=”5639820018″>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script></div>’;

    similar to

    <?php if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) : ?><amp-ad
    layout=”fixed-height”
    height=100
    type=”adsense”
    data-ad-client=”ca-pub-5367747358451280″
    data-ad-slot=”1919115743″>
    </amp-ad><?php else : ?><div class=”top-ad”><style>
    .gas_below_title { width: 320px; height: 100px; }
    @media(min-width: 500px) { .gas_below_title { width: 435px; height: 300px; } }
    @media(min-width: 800px) { .gas_below_title { width: 728px; height: 90px; } }
    </style>
    <script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
    <!– GetAndroidStuff Below Title –>
    <ins class=”adsbygoogle gas_below_title”
    style=”display:inline-block”
    data-ad-client=”ca-pub-5367747358451280″
    data-ad-slot=”2618604399″>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script></div><?php endif; ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Amp ads + Regular ads within content through functions in paired mode’ is closed to new replies.