Monday, September 12, 2022
HomeWordPress Developmentplugin improvement - Present adverts after second paragraph of the posts in...

plugin improvement – Present adverts after second paragraph of the posts in a selected class


I’m making an attempt so as to add an AdSense advert after the second paragraph of posts of a sure class. Following perform is not working for me; please assist:

//Insert adverts after second paragraph of single publish content material.
$catarray = get_the_category( $post->ID );
foreach ($catarray as $cat) {
$catid = $cat->term_id;

if ($catid == 124);

add_filter( 'the_content', 'prefix_insert_post_ads' );  
perform prefix_insert_post_ads( $content material ) {
  
$ad_code="<div>Insert Advert code right here</div>";

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


  // Dad or mum Operate that makes the magic occur

perform prefix_insert_after_paragraph( $insertion, $paragraph_id, $content material ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content material );
foreach ($paragraphs as $index => $paragraph) {

    if ( trim( $paragraph ) ) {
        $paragraphs[$index] .= $closing_p;
    }

    if ( $paragraph_id == $index + 1 ) {
        $paragraphs[$index] .= $insertion;
    }
}
  
return implode( '', $paragraphs );
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments