[Resolved] Insert Ad after first para

Home Forums Support Insert Ad after first para

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1033299
    Jagdish

    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

    #1033547
    Leo
    Staff
    Customer Support

    Hi there,

    You will need to expand your conditional tag to exclude WooCommerce:
    https://docs.woocommerce.com/document/conditional-tags/

    #1033587
    Jagdish

    Thanks for the reply.

    I copied this code from somewhere and not expert enough to add conditional tags.

    Please help to add that.

    #1033593
    Leo
    Staff
    Customer Support

    Try replacing this line:
    if ( is_single() && ! is_admin() )

    with this:
    if ( is_single() && ! is_admin() && ! is_woocommerce() )

    #1033602
    Jagdish

    It Worked!

    Thank you Leo.

    #1033603
    Leo
    Staff
    Customer Support
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.