Site logo

Archived topic

How to use snippets on specific posts?

5 replies · Started by Shoumya on October 31, 2021

Viewing posts 1–6 of 6

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?

ok thanks

No problem :)

This archived topic is closed to new replies.