
I want to insert an ad generated by the adrotate plugin, I want to insert it after the third post in the list of a specific category, take the following code as an example and I wanted to change it but it does not work for me.
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$adrotate = adrotate_ad(3);
$ad_code = $adrotate;
//*ORIGINAL CODE
/*if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}*/
if ( is_category('Financial') && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
//*ORIGINAL CODE
//$closing_p = '</p>';
$closing_p = '</article>';
$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 );
}
I appreciate your help
Read more here: How to Insert Ads within list post Category in WordPress