in

Show Ads between Post Content in WordPress

Display Google Adsense ads or Show other ad into WordPress Post or pages between the content there is no plugin requirement or without using it Advertising placement is necessary for webmasters to earn revenue for what they are doing hard work these code are works simply on you WordPress Theme like in function.php or single.php here are some methods.

Method 1: Display Ads Before or After Post Contentwordpres-code

  • Open Single.php or page.php in WordPress theme
  • Find <?php the_content(); ?> 
  • Paste you Ad Code before this show ad above content
  • And After this to show ad below post content
  • It’s simple method to show ads before and after the content of post.

Method 2: Show Ads Between Post Content

  • Again Open Single.php or in Template-parts/content-single.php file
  • find for <?php the_content(); ?> 
  • Replace this with below code
<?php
$block = 2; // this indicate that ad will appear after the 3rd paragraph
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for($i = 0; $i < count($content); $i++ ) {
if ($i == $block) { ?>
<div style="margin: 10px 0;"><b>YOUR AD CODE GOES HERE</b></div>
<?php }
echo $content[$i] . "</p>";
} ?>

 

Change $block value according to you it refer to after how many paragraph ads will show and YOUR AD CODE GOES HERE with your ads code you wanted to put

Method 3: Same as above

  • Copy and paste the following code into your theme’s functions.php:
function wrock_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', 'wrock_ad_text_after_n_chars');
  • 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.

Methods : Posted by Syed Balkhi

  • Open your WordPress Theme function.php file and Paste below code into it
//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 );
}

Replace Ads code goes here with you ad code then Save and Enjoy

Leave a Reply

Your email address will not be published. Required fields are marked *

Import Website Post From Joomla to WordPress

Show Recent Post in Blogger with Thumb Image