Bootstrap Templates Bundle

Sunday, April 14, 2013

Insert Ads within your Post Content in WordPress


Technic 1:

Copy and paste the following code into your theme’s functions.php:

function inject_ad_text_after_n_chars($content) {
  // only do this if post is longer than 1000 characters
  $enable_length = 1000;
  // insert after the first </p> after 500 characters
  $after_character = 500;
  if (is_single() && strlen($content) > $enable_length) {
    $before_content = substr($content, 0, $after_character);
    $after_content = substr($content, $after_character);
    $after_content = explode('</p>', $after_content);
    $text = '
      <!-- PUT YOUR AD HERE -->
    ';
    array_splice($after_content, 1, 0, $text);
    $after_content = implode('</p>', $after_content);
    return $before_content . $after_content;
  }
  else {
    return $content;
  }
}
add_filter('the_content', 'inject_ad_text_after_n_chars');


Modify:

Make sure to replace “PUT YOUR AD HERE” with your advertisement code.
$enable_length: Your ad will only be inserted if the post length exceeds this value.
$after_character: Your ad will be inserted after the first </p> after this value.


Technic 2:
Open your theme’s functions.php or a site-specific plugin file and paste the following code:

<?php
 
//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
	
	$ad_code = '<div>Ads code goes here</div>';

	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_code, 2, $content );
	}
	
	return $content;
}
 
// Parent Function that makes the magic happen
 
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 );
}

To add your ad code, simply edit $ad_code value where it says “Ad code goes here” on line 9. Once you do that, you are done. To change the paragraph number, simply change the number 2 to another paragraph number on line 12.
We hope that this article helped you insert ads within your post content in WordPress. You can re-use this function to add other items for example another ad after the seventh paragraph or so on.
Technic 3:

Display ads on the sidebar

  • Open Dashboard. Choose Appearance > Widgets.
  • Drag the 'Text' widget from the Available Widgets to the Sidebar section.
  • Open the 'Text' widget in the Sidebar. Paste AdSpeed's serving code in the content area. ClickSave.

Display ads only on the homepage

You would use the condition tag is_home() in the homepage.php (or index.php).
<?php if(is_home()) { ?>
  SERVING CODE
<?php } ?>

Display different ads for each content category

Our system is very flexible in the way you sell and organize ads. In WordPress, you would need to use is_category(). These are several methods to organize the ads:
1. One zone per category
You can create multiple zones, one for each category and get the serving code for the zone to put into the WordPress template. Any/all ads linked to this zone will get served in the category. This is the easiest solution to setup and manage. However, if you have many many categories then setting up the first time might take some time. To address this labor-intensive operation, you can use our API (application programming interface) to create the zones in batch.
2. URL targeting
You can create a few zones based on ad location (eg: top, right, bottom) that are shared across categories. The ads linked to these zones then have URL targeting. For example: only display AdA when the URL has "CategoryABC"). This requires that the URL follows a pattern that contains the defined category.

Display category ads on an article/post

If a post is placed in one or more categories, you might want to display ads only for these categories. You would need to edit the single.php template file and use the function in_category() to check if the post is linked to certain categories.

<?php if (in_category('Travel')) { ?>
  [Serving Code for Category/Zone Travel]
<?php } ?>

2 comments:

  1. HI,
    I find many useful things herewith. It is really amazing for us. Where ever we are, just click on the website and Take advantages regarding place your ads.Your article impressed me very heartily. From a last decade I was searching such type of web site. Thanks for giving me a space in your blog. I refer this to my close friends who are already seeking for the same .

    ReplyDelete
  2. Hey Jeff Hardey hw r u. try this website where u can Buy / Sell all items at world's largest 2nd Hand shop http://bechdaal.com with 100% delivery guarantee.

    ReplyDelete

Murphy - Home Maintenance & Repair Service HTML Template

Murphy is a handyman repair service Bootstrap HTML Template. It is ideal for either all-rounders or professional teams to introduce the...