Hi chinty74, Just to summarise for other readers: you want to show a different advert above the post title, based on the post category. This isn’t supported by the Ad Injection UI, but Ad Injection allows PHP in its ad boxes. I have provided an example of loading ads based on categories, but you now want to extend it to randomly choose from a pool of ads for each category.
—
Yes you would need to modify your template to do this – you can use an Ad Injection template ad tag above your post title to do this. Ad Injection will generate an example template ad tag for you (with the correct path) – look in the ‘test ads and template ad examples’ at the bottom of the main UI.
The problem with one text file for each rotated ad in each category is that your PHP code has to know how many ads there are. Here’s some options:
1. You could hard code the number of ads for each category into the PHP, but then the code would need updating if you changed the number of ads.
2. The PHP code could count the number of ads with the name [category][number].txt assuming that all numbers are sequential and start from 1 – this would be more flexible.
If you did store the ads in individual text files I’s suggest you put a number after *each* ad so travel1.txt, travel2.txt etc. If you called the first one travel.txt with no number then the names would be inconsistent.
In your PHP code you could then generate a random number between 1 and the number of ads you have, and append this to the name of the ad file which the PHP is loading.
If you know a bit of PHP this shouldn’t be too hard. If you come up with a solution do share it!
I might have a go at this myself in the next week. If I come up with a solution I’ll post it here and in the FAQ.
Hi chintu74, I’ve extended my code to do what you want. If for example the category nicename is ‘liverpool’ the code will find all the text files in the ad directory with ‘liverpool’ in the file name, and will then randomly select one. It doesn’t matter what naming convention you use for the ad files – as long as the nicename is in there. You can find the example code on advancedhtml – it is the second example.
Thumbs up! I would check it soon. Thank you 🙂
One question: does the new feature work on wordpress 2.9.2? This particular blog is of that version (due to another plugin that I have to use here but it is not compatible beyond wp 2.9.2). Let me know.
Hi chintu74, I would guess it should work on WordPress 2.9.2, but I have only tested it on 3.2. Do let me know if it works for you!