How to insert ads between YARPP and post content
You may have a hard time while trying to insert ads – such as Google Adsense – between your YARPP (Yet Another Related Posts Plugin) section and your post content in WordPress, like this:
Fear no more, here is the solution for you! The following code has been tested only with YARPP, but it is supposed to work with many other related posts plugins for WordPress.
First of all, open the file functions.php from your active theme folder with your favourite code editor (or WordPress integrated one) and add these lines of code at the end:
// BEGIN CUSTOM CONTENT AFTER POST function custom_content_after_post($content){ if (is_single()) { $content .= <<<EOT EOT; } return $content; } add_filter( "the_content", "custom_content_after_post" ); // END CUSTOM CONTENT AFTER POST
Pay attention to not put anything (spaces, tabs, etc.) on the rows containing EOT strings, or the script will simply break up. Just copy-paste it without modifications and insert your adsense code in the rows between the two EOT occurences.
It should look like this:
// BEGIN CUSTOM CONTENT AFTER POST function custom_content_after_post($content){ if (is_single()) { $content .= <<<EOT <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- Ads below post --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-506503814513999" data-ad-slot="1119430145" data-ad-format="auto"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> EOT; } return $content; } add_filter( "the_content", "custom_content_after_post" ); // END CUSTOM CONTENT AFTER POST
The is_single() statement ensure that ads are shown only on post pages.
If instead you want to show ads on different types of pages (page, article, etc.) you can replace it with the corresponding check among the following:
- is_page ()
- is_home()
- is_tag()
- is_search()
- is_archive()
- is_category()
- is_front_page()
For any doubts or questions feel free to leave a comment in the form below.
0 Comments