Found in the vast network code display ads, and how to add exception to the output in a post?

Good day, dear guru.

As found in the vast network code to add exception to display advertising entries for a particular post (or several posts)?

<?php

//Insert the ad unit after the second paragraph of a particular article.

add_filter( 'the_content', 'prefix_insert_post_ads' );

prefix_insert_post_ads function( $content ) {

 $ad_code = '<div-->Ad code is placed here';

 if ( is_single() && ! is_admin() ) {
 return prefix_insert_after_paragraph( $ad_code, 2, $content );
}

 return $content;
}

// Parent function that allows the magic to happen

prefix_insert_after_paragraph function( $insertion, $paragraph_id, $content ) {
 $closing_p = '<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 );
}
June 14th 19 at 20:43
1 answer
June 14th 19 at 20:45
Solution
If the post is no ads 1, it is possible for aydishnik post:
global $post;
 if ( is_single() && ! is_admin() && ($post->ID != 5)) ) {
 return prefix_insert_after_paragraph( $ad_code, 2, $content );
 }


Well or several ajtishnikam

global $post;
$without_ad = [1,4,6,7]
 if ( is_single() && ! is_admin() && !in_array($post->ID, $without_ad)) ) {
 return prefix_insert_after_paragraph( $ad_code, 2, $content );
 }

Array ajtishnikam can be deduced from the admin panel. Can custom field of the post.

Can accessories post to a specific category.
global $post;
 if ( is_single() && ! is_admin() && !in_category($cat_id, $post)) ) {
 return prefix_insert_after_paragraph( $ad_code, 2, $content );
 }
Many thanks for your comprehensive answer :-) - nikita.Stracke commented on June 14th 19 at 20:48

Find more questions by tags WordPress