Archived topic
How to use snippets on specific posts?
5 replies · Started by Shoumya on October 31, 2021
Hi,
I am following this blog: https://www.wplogout.com/insert-google-adsense-ads-in-generatepress/ to add adsense adsense without any plugin.
But I do not want to show ads in 3-4 posts. How can I do this?
Hi there,
if you're using the Hook method, then edit the Hooks Display Rules > Exclusions - and add the posts you don't want the scripts to load on.
I am not using hook. I have a code in code snippet.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = ' Paste your AdSense Code Inside this quote';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 4, $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 );
}
this code is supposed to add ads after the 4th para of every post. Now I do not want to show ads on 2 posts. How can I do that?
We cannot help with custom solutions here.
https://generatepress.com/what-support-includes/
You can check with the solution author - looks like he's quite active in the comments section of his post.
ok thanks
No problem :)