How can I call a different sidebar for every page in Wordpress?

Status
Not open for further replies.

Unarmed Gunman

Medium Pimpin'
May 2, 2007
7,335
287
0
The D
www.googlehammer.com
I am throwing together a "Sponsor this page" type of thing on one of my Wordpress based sites to sell targeted advertising. I've got a good spot on the sidebar where I would like to display the banner ad of whoever the page sponsor is, but with 1800+ pages, I can't do it with the default dynamic sidebar.

Is there a plug-in, or something else I can do that would allow me to display a different sidebar for each page? I'd hate to have 1800+ sidebars, so maybe there would be an easier way to do this? The sidebar should look identical, just with a different ad in that 250x250 adspace. (Actually, I may shoot for 4 ads of 125x125 in the space instead).

Any ideas would be greatly appreciated.
 


Use a different page template for each sponsored page

Code:
<?php
/*
Template Name: Sponsor01
*/
?>
Basically, copy and paste your current page and edit the sidebar in your new page templates (ex. Sponsor01Page.php, Sponsor02Page.php)

Another option, set up OpenX and define zones for your advertisements. Have run of site be the default, but on sponsored pages you could opt to just display select advertiser(s).
 
Here's one more idea, set up a default sidebar that calls the custom field of the page (2.5+ wordpress). The custom field could be Key: sidebarAd, Value: <div class="sidebar250x250ad"><a href=""><img src="url" alt=""/></a></div> or you could use multiple keys, one for each url and image and add all your html in your sidebar.php and just plug in the values.

Code:
<?php $urlvalues = get_post_custom_values("url"); echo $urlvalues[0]; ?>

<?php $imgAd = get_post_custom_values("adImage"); echo $imgAd[0]; ?>

That would leave you with one sidebar and you're managing your advertisers w/in the pages.
 
Sure you can do it with the default dynamic sidebar.

You could use either one of radio's suggestions and either use something like the PHP Code Widget or you can mix dynamic and static sidebars by removing the else statement.
 
Last edited:
Wordpress can determine the page. I know this works in the hook, but not sure about the side bar. You could try:

PHP:
if (is_page('whatever')) {
                include('thissidebar.php');
                } else {
                include('defaultsidebar.php');
                }
You can try to just echo the page name to see if it works in the side bar. I would test it for you but I am on my way out right now.

edit: You can probably uses this in index.php where the side bar is included. Not 100% positive though.
 
I was going to suggest the above too, using if, elseif, etc seems to be the best way to do it.
 
Status
Not open for further replies.