0

I just learned (with help from Rob Vermeer!) how to use one template and be able to call different top graphics depending on the page and now I want to know if the following is possible:

I'm using the page.php (default template) for several pages. Layout for those pages are the same but sidebar content on each page will be different. Rather than having to create separate page templates simply to be able to call different sidebars, can I write a function that would specify "if this page, call this sidebar"?

At the codex, on this page http://codex.wordpress.org/Function_Reference/get_sidebar it has a section: Multi Sidebars. I tried adapting that code but it doesn't seem to work. It seems to be what I want, but in the examples it's using stock pages and my pages have custom names -- don't know if that's problem.

Also, I'm calling the sidebars after the loop - don't know if that's the problem?

My current (not working) code is:

<?php
if ( is_customer() ) :
    get_sidebar('customer');
elseif ( is_vendors() ) :
    get_sidebar('vendors');
elseif ( is_services() ) :
    get_sidebar('services');
else :
    get_sidebar();
endif;
?>

I have sidebars called sidebar-customer, sidebar-vendors, sidebar-services, and pages called customer, vendors, services, etc. I have the code in the page.php template in the place where, if I had one sidebar I would simply have:

<?php get_sidebar(); ?>

Help greatly appreciated!


@m0r7if3r I think you're coming the closest to what I'm trying to do but not exactly.

The site is not a blog, first of all. It consists of several "static" pages. I have a homepage template just for the home; the page.php (default) template for most of the internal pages, a 404 template and a small one-column template for little pages.

The page.php template has 2 columns - a wider area for the main content, and a narrow column to the right. I want to use the right columns as "sidebars" but the content of the sidebars will be different for each page.

So, say I have pages like: Customer Service, About Us, Services, and Contact. I want to use the page.php template for all of them, but the sidebar content will be different for each. In the past I would register 4 (or 5 or however many I needed) sidebars, and then create separate templates, the only difference in the templates being that they call different sidebars. I just wondered if there was a neater way of doing this.

My idea was that I would have a sidebar that went with each page and basically would do an if/then in the page.php where I would normally call the sidebar: if this is the customer service page, call the customer service sidebar; if this is the about page, call the about sidebar.

But I don't know if that's possible and if it is, precisely what the exact steps and wording would be. I don't know if I'd use the page names, ID's, etc. I assume I would need the created pages and the registered sidebars but after that I'm not sure what to do. I thought it was possible because of the "Multi Sidebars" section on the "get sidebar" functions page in the codex but I'm probably misunderstanding it. I have the link in my first entry; scroll down on the page and see Multi Sidebars.

? Thanks!

1
  • Did you write the is_XY() "conditional functions" yourself?
    – kaiser
    Commented Jan 16, 2012 at 0:47

5 Answers 5

-1

If you're using static pages, try using is_page() for your conditionals. Let's assume your static pages have slugs, "customers", "vendors", and "services":

<?php
if ( is_page( 'customers' ) :
    get_sidebar('customer');
elseif ( is_page( 'vendors' ) ) :
    get_sidebar('vendors');
elseif ( is_page( 'services' ) ) :
    get_sidebar('services');
else :
    get_sidebar();
endif;
?>

But I agree with @markratledge that you're probably better-served using a single sidebar, and Widget Logic.

1
  • I think this is what I was looking for - I wondered if I could use the is page - Thanks!
    – PVA
    Commented Jan 16, 2012 at 20:48
3

I have a function in my themes for this:

/**
 * Returns a string for the current template’s context.
 * Used by get_template_part()
 *
 * @return string
 */
function t5_template_context()
{
    // internal cache
    static $return = NULL;

    if ( ! is_null( $return ) )
    {
        return $return;
    }

    is_year()             and $return = 'year';
    is_month()            and $return = 'month';
    is_day()              and $return = 'day';
    is_search()           and $return = 'search';
    $pt = get_post_type() and $return = $pt; // FALSE or string.
    is_attachment()       and $return = 'attachment';
    is_front_page()       and $return = 'front-page';
    is_404()              and $return = '404';
    is_null( $return )    and $return = '';

    return $return;
}

Now, in my templates I use it like this:

complete index.php

<?php # -*- coding: utf-8 -*-
get_header( t5_template_context() );

get_template_part( 'loop', t5_template_context() );

// Loads the sidebar too.
get_footer( t5_template_context() );

I took this idea from mfields’ post Adding Context to Template Parts.

2

Different solution than multiple sidebars and probably easier: WordPress › Widget Logic

2

Do you have the sidebars registered in your functions.php file?

Are the conditional statements you are using correct? Did you write a function called is_customers() and the others yourself? WordPress does not come with this by default. Since your code to show the sidebars is in page.php, only is_page() would return true since it's already a function in WP.

If you're trying to see if the page you're on is a custom post type, you would use something like this:

Edited (a switch would be better to understand probably)

<?php
switch( get_post_type() ) {
    case 'customer': get_sidebar( 'customer' ); break;
    case 'vendors': get_sidebar( 'vendors' ); break;
    case 'services': get_sidebar( 'services' ); break;
    default: get_sidebar();
}
?>

The example displays how to switch what sidebar to show based on get_post_type() and has a default callback as well.

Otherwise you would need to use a function that WP already has built in, or create your own. Always check the WordPress Codex for valid functions to use.

1

I've used the following before:

if( !dynamic_sidebar( 'archive-sidebar' ) ) {
    if( !dynamic_sidebar( 'post-sidebar' ) ) {
        dynamic_sidebar( 'page-sidebar' );
    }
}

Basically what this does is allow the displayed sidebar to fallback so if 'archive-sidebar' doesn't exist, 'post-sidebar' is used, and if that doesn't exist, 'page-sidebar' is used. Moral is, you can very easily apply logic to sidebars.

I think your error may be that you're using get_sidebar() instead of dynamic_sidebar(). dynamic_sidebar() uses the sidebars you register, whereas get_sidebar() looks for a file. So, get_sidebar( 'customer' ); would look for sidebar-customer.php.

2
  • Hi! I think you're coming the closest to what I'm trying to do but not exactly. The site is not a blog, first of all. It consists of several "static" pages. I have a homepage template just for the home; the page.php (default) template for most of the internal pages, a 404 template and a small one-column template for little pages.
    – PVA
    Commented Jan 16, 2012 at 16:36
  • Which part of it doesn't suit you?
    – mor7ifer
    Commented Jan 16, 2012 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.