如何在wordpress单页面中添加广告代码而不影响第二个单页模板

I am using two different template for single page. One with sidebar and another one without sidebar.

Now i want to inject the ad code only in the sidebar template.

Could any one give me a code for this one.

Note:

  1. The ad is automatically inserted after every second paragraph. And i have the code.

    add_filter( 'the_content', 'prefix_insert_post_ads' );

    function prefix_insert_post_ads( $content ) {
    
    
            if ( is_single() && ! is_admin()) {
                return prefix_insert_after_paragraph( $ad_code, 2, $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 above code display the ad code both in sidebar template and no sidebar template. But actually i want to display the ad code only in sidebar template. Could any one please help me how to do it.

You can check which template is used with get_page_template_slug():

    if ( is_single() && ! is_admin() && get_page_template_slug() == 'template-sidebar.php') {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

You can use this code on your single php file

if (is_page(12)) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

Thanks.