Add custom content after WordPress article paragraphs

WordPress six five hundred and twenty-eight Reading mode

If you want to add custom content after a WordPress article paragraph, such as an advertisement after the second paragraph of the article, you can try the following code:

 Add custom content after WordPress article paragraphs

Add custom content after WordPress article paragraphs

section
 //Insert content after the second paragraph of the article add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { $ad_code='<div>added text</div>'; if ( is_single() && !  is_admin() ) { return prefix_insert_after_paragraph( $ad_code, 2, $content ); } return $content; } //Main function 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 ); }
section

Add different contents after paragraphs 2, 4 and 6 respectively

 add_filter( 'the_content', 'add_ads_to_content' ); function add_ads_to_content( $content ) { $ads = array( 2 =>  'ad code 1', // paragraph_id => ad_code 4 => 'ad code 2', // paragraph_id => ad_code 6 => 'ad code 3' // paragraph_id => ad_code ); if ( is_single() && !  is_admin() ) { foreach ($ads as $paragraph_id => $ad_code) { $content = prefix_insert_after_paragraph( $ad_code, $paragraph_id, $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 ); }
section

If it is only displayed and added in a classified article, you can also write this

 add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $content ) { $ad_code='<div>custom text</div>'; if ( is_single() && !  is_admin() && in_category('29') ) { return prefix_insert_after_paragraph( $ad_code, $content ); } return $content; } function prefix_insert_after_paragraph( $insertion, $content ) { $closing_p = '</p>'; $paragraphs = explode( $closing_p, $content ); $paragraph_id = round(count($paragraphs) / 2); foreach ($paragraphs as $index => $paragraph) { if ( trim( $paragraph ) ) { $paragraphs[$index] .= $ closing_p; } if ( $paragraph_id == $index + 1 ) { $paragraphs[$index] .= $ insertion; } } return implode( '', $paragraphs ); }

Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!

 weinxin
My Wechat
Copyright Notice
Please indicate the source and link of the original article reprinted on this site. Thank you for your cooperation!
five hundred and ninety-eight million eight hundred and forty-five thousand and six
 
 Robin
five hundred and ninety-eight million eight hundred and forty-five thousand and six
Comments six    Visitors four    Author two
    •  net friend
      net friend two

      Add a gadget at the end of the article instead of paragraph 2. How should I change it

        •  Robin
          Robin

          @ net friend https://zmingcx.com/add-widgets-to-wordpress-posts.html

            •  net friend
              net friend two

              @ Robin thank you

              •  net friend
                net friend two

                @ Robin Can you share the content of the article and add the code of the gadget at the end

            •  I'm lazy to death
              I'm lazy to death zero

              Add how to implement each paragraph

             anonymous

            Comment

            Anonymous netizens
             :?:  :razz:  :sad:  :evil:  :!:  :smile:  :oops:  :grin:  :eek:  :shock:  :???:  :cool:  :lol:  :mad:  :twisted:  :roll:  :wink:  :idea:  :arrow:  :neutral:  :cry:  :mrgreen:

            Drag the slider to complete validation