$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>filters - How to hide Ads in between posts on AMP?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

filters - How to hide Ads in between posts on AMP?

matteradmin12PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I am using the following code to show ads on my posts after Para 4:-

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
    $ad_code = My Ad code;
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 4, $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 );
}

The problem is the ads appear on AMP pages too. Not only ads, whatever I put in $ad_code starts appearing in AMP pages. I want this to show only on the_content for Non-AMP pages.

For AMP pages, I want to show a Newsletter form after Para 4. How can I achieve this? I do know I could add a display:none to the div on AMP pages. But, is there any other way?

And, I am using Wordpress's AMP plugin.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I am using the following code to show ads on my posts after Para 4:-

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
    $ad_code = My Ad code;
    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 4, $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 );
}

The problem is the ads appear on AMP pages too. Not only ads, whatever I put in $ad_code starts appearing in AMP pages. I want this to show only on the_content for Non-AMP pages.

For AMP pages, I want to show a Newsletter form after Para 4. How can I achieve this? I do know I could add a display:none to the div on AMP pages. But, is there any other way?

And, I am using Wordpress's AMP plugin.

Share Improve this question edited Feb 7, 2019 at 9:39 phatskat 3,1741 gold badge18 silver badges26 bronze badges asked Feb 7, 2019 at 6:55 droidtaberdroidtaber 132 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You should be able to do something like this in your filter function:

function prefix_insert_post_ads( $content ) {
    if ( is_admin() ) {
        return $content;
    }

    if ( ! is_single() ) {
        return $content;
    }

    if ( ! is_amp_endpoint() ) {
        $ad_code = My Ad code;
        return prefix_insert_after_paragraph( $ad_code, 4, $content );
    }

    // We must be on amp:
    return amp_insert_after_paragraph( ... );
}

Then define your amp_isnert_after_paragraph function to insert the newsletter where you want.

Post a comment

comment list (0)

  1. No comments so far