How to show adsense on specific posts/pages in WordPress?

Very often, you might want show or not show adsense in specific posts or pages. Infact you dont even  need to search for wordpress plugins to accomplish the job. The easy way is hack directly into wordpress code.

You can open your current wordpress theme and make changes to single.php, category.php accordingly where you want the adsense to appear..

To show only in single posts you have to first check using is_single() function and put the adsense code inside it. similary is_category() for categories.

For example the below will show your adsense code only in all single post pages.

[php]

<?php if  (is_single()) { ?>

………….
Adsense code here
……………

<?php } ?>

[/php]

If you specify a number is_single(’15’) it means that the code will show your adsense code only the post ID is 15.

Similarly goes for

  • is_category() —  To show in category pages
  • is_page() — To show in a wordpress page.
  • is_home() — To show only in homepage.

Another example if you want to show your adsense code in both homepage and single posts, you can use this code.

[php]
<?php
if (is_home() || is_single()) { ?>
———— Adsense code ————
<?php } else { ?>
———-Adsense code ———-
<?php } ?>
[/php]

More information about tweaking you can refer to this wordpress documentation