Does anybody know if there is some piece of code or a plugin that will selectively show Adsense on some blog posts and not others?
Any help would be greatly appreciated.
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
September 08, 2008 7:22am
Subscribe [2]#1 / Sep 08, 2008 7:22am
Does anybody know if there is some piece of code or a plugin that will selectively show Adsense on some blog posts and not others?
Any help would be greatly appreciated.
#2 / Sep 08, 2008 7:35am
Hi Peter,
I’ve never used Adsense so not too sure of what the code is that you have to use to show them but you could probably do this quite easily or have a good start at doing this by having a simple drop down custom field with just a Yes / No configuration and then some very simple conditional code in your template such as this :
{if show-adsense == "Yes"}
Place your adsense code hereā¦
{/if}show-adsense would be the short name of your custom field used as the drop-down menu.
Don’t know if that helps at all?
Best wishes,
Mark
#3 / Sep 08, 2008 8:14am
Many thanks.
I would like to automate this by specifying a conditional that allows adsense to appear in posts in certain categories but not other categories.
Any ideas?
#4 / Sep 08, 2008 8:30am
When you say posts in certain categories do you mean :
All the posts in certain categories
or
Some posts in certain categories
Using the drop-down method above then this will work for all posts and it doesn’t matter which category it is in the conditional will just show the code dependant upon what you select. That should usually be enough?
If on the other hand you have an entry that is in more than one category and you choose Yes in the drop-down but only want it to show in say one of the categories that it is in then you could possibly extend the conditional to do that too.
All depends on exactly what you need really though. Perhaps try giving the conditional a go first though along with the drop-down as you may find that it does exactly what you need anyway 😉 Easy to get rid of it or modify it later on if you do indeed need something a little more complex though.
Best wishes,
Mark
#5 / Sep 08, 2008 8:40am
I mean “All the posts in certain categories”
I’m just a bit concerned that this might be labour intensive going back through many hundreds of individual posts to check yes or no. The catch all, if it’s possible, would be a conditional based on certain categories and to remove adsense code from those posts that appear in those categories.
I hope that makes sense 😊
#6 / Sep 08, 2008 8:55am
Hi Peter,
Aha I get what you need now 😉
I think that the code here should hopefully get you what you need.
{exp:weblog:entries weblog="default_site"}
<h2>{title}</h2>
<p>{exp:weblog:categories weblog="default_site" style="linear"}<br />
{if category_id == '1' OR category_id == '2'}<br />
Show adsense code for only items in category 1 or 2<br />
{/if}<br />
{/exp:weblog:categories}</p>
<p>{/exp:weblog:entries}You will need to find out the ids of your categories yourself though but I’m pretty sure this should work.
Best wishes,
Mark
#7 / Sep 08, 2008 8:56am
Many thanks. I’ll give it a whirl.
#8 / Sep 08, 2008 8:58am
Actually I’m pretty sure that the code above will work but unfortunately you would only be able to check against two category_ids. To check against more than that then you would need a succession of code conditional blocks such as this :
{exp:weblog:entries weblog="default_site"}
<h2>{title}</h2>
<p>{exp:weblog:categories weblog="default_site" style="linear"}</p>
<p>{if category_id == '1' OR category_id == '2'}<br />
Show adsense code for only items in category 1 or 2<br />
{/if}</p>
<p>{if category_id == '3' OR category_id == '4'}<br />
Show adsense code for only items in category 3 or 4<br />
{/if}</p>
<p><br />
{/exp:weblog:categories}</p>
<p>{/exp:weblog:entries}Don’t think that it will cause any problems although I could be mistaken and I’m sure someone will pick me up on this if I am 😉
Depending on how many categories you need to check against then this might be handy or else you might want to look into creating a plugin to do the same sort of thing for you.
Best wishes,
Mark
#9 / Sep 08, 2008 9:47am
Rather strangely, this code seems to remove the code for the category specified - rather than show the code for the category specified. As it happens this is what I want to happen.
But, is this what this code is supposed to do?
#10 / Sep 08, 2008 10:17am
Hi Peter,
Not too sure what you mean by removing code?
If it’s doing what you need though then that’s good then? 😉
Let me know what it is doing though as I’m not too sure what you meant by removing code.
Best wishes,
Mark
#11 / Sep 09, 2008 6:20am
Hi,
I seem to have go this working just fine now. However, one problem - it seems to strip the adsense of all formatting for some reason. Very odd.
Any ideas how to rectify this?
#12 / Sep 09, 2008 1:52pm
Hi,
Could really do with some help.
The code below works just fine at conditionally showing adsense by category name. However, when it does show the adsense, it seems to strip it of any formatting i.e it loses it’s colour, style and shape. Really very puzzled why this should be.
If anybody has an idea for rectifying this, or an alternative way of doing this by another way e.g php - I would be massively grateful.