WordPress Implementation inserts the advertisement Federation code in any paragraph position of the article

Source: Internet
Author: User

Insert Post ads is a very simple and easy-to-use plug-in that can be installed online by searching for Insert post ads in the background plugin installation interface. You can insert multiple different ads in different paragraphs in the article and on the page, all you need to do is create a new ad in the background.

Then select the inserted paragraph position.

Plug-in method, there is nothing much to say, simple and easy to use.

Pure code to achieve WordPress in the article arbitrary paragraph position Insert Advertising Federation Code

Add the following code to the functions.php of the current topic:

The code is as follows Copy Code

Add_filter (' The_content ', ' prefix_insert_post_ads ');
Function Prefix_insert_post_ads ($content) {
  $ad _code = ' <div> Add your ad code </div> ';
 if (Is_single () &&! is_admin ()) {
 //modifies 2 of this paragraph
 return prefix_insert_after_paragraph ($ad _code, 2, $content);
 }
 r Eturn $content;
}
//Insert the functional code required by the advertisement
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;
 }
 
& Nbsp;return implode (", $paragraphs);
}

The tribe here needs to add that we modify the ad code and the paragraph as described in the above comments.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.