Ask your WordPress questions! Pay money and get answers fast! (more info)

How do show a AD in the side bar if certain Category & Posts WordPress

How do I get an add to show a AD in the side bar only if is a certain category ID and if the post is located in that category. And if there are 2 categories make sure it does not show 2 ads. I have 7 Categories with 7 different AD CODES. I was using a IF_ on the conditional tag and it would show 2 ads so I used the IN_ but now it does not show them on the POSTS.

CURRENT AD CODE in a conditional tag

<?php if (in_category('category')) : ?>
<!--/* isocket Javascript Tag v2.0 */--><script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'':'');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=3661&amp;block=1");
document.write ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='' target='_blank'><img src='' border='0' alt='' /></a></noscript>
<?php endif; ?>

Answers (3)


Kiet Luong answers:

can you show full code of that page ?

Jay comments:

I can through email - what is your email again?

Kiet Luong comments:

it is [email protected], please add it to your list, :)


Abdelhadi Touil answers:

Try this code works (tested):

<?php if (is_category('category1') || (is_single() && in_category('category1'))) { ?>
Ad's code here for 'category1'
<?php } elseif (is_category('category2') || (is_single() && in_category('category2'))) { ?>
Ad's code here for 'category2'
<?php } ?>

and you can contiue with the same elsif condition as you like.

You can also use Simple Ads Manager, it can do what exactly you are looking for and more:

[[LINK href=""]][[/LINK]]

It's very useful, except if you want it using a code, so try the first one.
Good luck.

Jay comments:

So if I have 7 ads will this allow me to FORCE them per category, but they will still show up in the POSTS? I just confused about how if the post is selected in multiple categories..

Seems like I would only have to do this once for the extra category that gets used sometimes. If that does not make sense I will reply in a bit, need more coffee.

Abdelhadi Touil comments:

Hi. As I see if a post has more than one category then I think the ad will be shown on all these categories. Just test it please and don't forget to tell us what happen with you :)
Good luck.


MDan answers:

Hi Jay,

I recommend this plugin: