Archived topic
Insert Ad after first para
5 replies · Started by Jagdish on October 13, 2019
I am using this code to insert ad code after first para in every post.
//Insert AD after second para of the post
add_filter( 'the_content', 'post_ads' );
function post_ads( $content ) { $ad_code = '<div>
AD CODE HERE
</div>';
if ( is_single() && ! is_admin() )
{ return ad_after_para( $ad_code, 1, $content ); }
return $content; } // Function that makes this Work
function ad_after_para( $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 ); }
Now I added woocommerce in the site. Same ad is appearing on product pages too.
I need this ad only on blog posts.
Please suggest the modification in this snippets to achieve this.
Thank You
Hi there,
You will need to expand your conditional tag to exclude WooCommerce:
https://docs.woocommerce.com/document/conditional-tags/
Thanks for the reply.
I copied this code from somewhere and not expert enough to add conditional tags.
Please help to add that.
Try replacing this line:
if ( is_single() && ! is_admin() )
with this:
if ( is_single() && ! is_admin() && ! is_woocommerce() )
It Worked!
Thank you Leo.
No problem :)