You may have noticed that we’ve tweaked how we display our adsense ads. We wanted some small text-only ads near the bottom of each article, which would be added automatically. This took a little bit of tinkering, but eventually we developed a solution which works well and doesn’t seem to knock page processing times much. Simply add the following code to your theme’s functions.php (don’t forget to change your adsense IDs)
Updated 12-Nov-2017 as the code wasn’t working correctly as previously displayed. मैं भी जोड़ दिया है 2 other functions that I use, one to insert adverts via shortcode, and another to add an advert where the more is removed when a post is shown in full
/************************************************************************\ * की प्रविष्टि के लिए ऐडसेंस शॉर्ट कोड 2 शोर्ट के माध्यम से कहीं भी छोटे विज्ञापन * \************************************************************************/ समारोह Google_Adsense( $Atts, $सामग्री = बातिल ) { वापसी '<div class ="ए" शैली ="पाठ संरेखित: केंद्र;"><div id ="गूगल-1"></div><p style ="स्पष्ट: दोनों"></पी></div>'; } add_shortcode('गूगल ऐडसेंस', 'गूगल ऐडसेंस'); /******************************************\ * दिखाना 2 अधिक टैग पर छोटे adsense विज्ञापन * \******************************************/ add_filter('सामग्री', 'Adsense_added_at_more_tag'); // यह समारोह अपने ऐडसेंस कोड के साथ अपने अधिक टैग को बदलने. समारोह adsense_added_at_more_tag($टेक्स्ट) { अगर( अकेला है() ) : $ads_text = '<div id ="गूगल" वर्ग ="ए" शैली ="पाठ संरेखित: केंद्र;"><div id ="गूगल-1"></div><p style ="स्पष्ट: दोनों"></पी></div>'; $pos1 = strpos($टेक्स्ट, '<span id ="अधिक-'); //" अगर($pos1 === FALSE) $पाठ = $ ads_text . $टेक्स्ट; अन्य { $pos2 = strpos($टेक्स्ट, '</विस्तार>', $pos1); $टेक्स्ट 1 = substr($टेक्स्ट, 0, $pos1); $टेक्स्ट 2 = substr($टेक्स्ट, $pos2 + 7); $पाठ = $ टेक्स्ट 1 . $ads_text . $टेक्स्ट 2; } अगर अंत; वापसी $ पाठ; } /*******************************************************\ * अंतिम अनुच्छेद ऊपर एक बड़ी ऐडसेंस विज्ञापन दिखाएं * \*******************************************************/ add_filter('सामग्री', 'Gads_added_above_last_para'); समारोह gads_added_above_last_para($ytext) { अगर( अकेला है() ) : $yads_text = '<div class ="ए" शैली ="पाठ संरेखित: केंद्र;"><div id ="गूगल-2"></div><p style ="स्पष्ट: दोनों"></पी></div>'; अगर($ypos1 = strrpos($ytext, '</पी>')){ $ytext1 = substr($ytext, 0, ($ypos1 + 4)); $ytext2 = substr($ytext, ($ypos1 + 4)); $ytext = $ ytext1 . $yads_text . $ytext2; } अगर अंत; वापसी $ ytext; }
I don’t know but your code was not working in my case. इतना, I replaced it with other code. If anyone else is facing the same problem, then you can use the below code.
function insert_ad_block( $टेक्स्ट ) {
अगर ( अकेला है() ) :
$ads_text = ‘My Ad Code Here’;
$split_by = “n”;
$insert_after = 2; //number of paragraphs
// make array of paragraphs
$paragraphs = explode( $split_by, $टेक्स्ट);
// if array elements are less than $insert_after set the insert point at the end
$len = count( $paragraphs );
अगर ( $लेन < $insert_after ) $insert_after = $len;
// insert $ads_text into the array at the specified point
array_splice( $paragraphs, $insert_after, 0, $ads_text );
// loop through array and build string for output
प्रत्येक के लिए( $paragraphs as $paragraph ) {
$new_text .= $paragraph;
}
return $new_text;
अगर अंत;
वापसी $ पाठ;
}
add_filter(‘the_content’, ‘insert_ad_block’);
Is there a way to do this say after the 2nd paragraph instead of before the last one?
Absolutely. You would just need to tweak the code slightly. I’m using
strrpos
which finds the last occurrence of<p>
If you wanted to find the second occurrence of
<p>
replace line 13 from the code above with…Great work man. बहुत बहुत धन्यवाद. Do you have any idea how to place a horizontal line above and below the ad unit?
ज़रूर. Add a couple of <hr />‘s — 1 at the start of the value of $ads_text and the other at the end.
उदा.. replace
साथ