Treehouse: Grow your CSS skills. Land your dream job.

WordPress custom template, custom sidebar?

  • cjk
    # April 5, 2009 at 7:41 pm

    Hey all,

    I have been playing with the custom template function in WordPress as I would like to use a special layout for a special section of pages.

    I have figured out how to create a custom page.php file and assign it etc., however, I have not been able to find in the codex (or figure out even if it is possible) whether I can grab a custom header.php sidebar.php that would only appear in pages that use the custom template.

    In a nutshell, I would like to use a different header and sidebar on pages that use the custom page template.

    Is there a way?

    Best regards,

    # April 5, 2009 at 10:36 pm

    Just create your custom sidebar/header and save it as sidebar2.php/header2.php or something equally imaginative and the call it/them in your custom template with

    < ?php include ('header2.php'); ?>
    < ?php include ('sidebar2.php'); ?>
    # April 7, 2009 at 10:25 am

    Hey thanks Apostrophe,

    I would have been surprised if there wasn’t a way to do this (seemed like a popular request for feature to me!) but couldn’t find it in the codex at all. You saved me a ton of time!

    I am going to assume that I directly replace the get_header and get_sidebar with these include calls?


    # April 7, 2009 at 10:42 am

    That’s right. get_header and get_sidebar are just WordPress specific functions relating to the sidebar and the header. Just place the includes where you would usually use the WP functions.

    # April 8, 2009 at 4:21 pm


    Close…. but not quite what I was hoping for. My sidebar.php is widget ready and so of course any widget that I have in the main sidebar shows up in any and all copies, if they are also widget ready (which i would really like if possible as there are some widgets I would like to use on certain pages only). I guess I could just strip the widget function out of those custom sidebars, but I would really like the functionality.

    So, I dug into the codex one more time and found this page where it explains how to;

    New way of adding sidebars

    You can register more than one sidebar. In wp-includes/widgets.php you find the function-definition for register-sidebars() which you may use in your custom function within your theme-folder (functions.php – if not existent, create an empty php-file by that name):

    < ?php if ( function_exists ('register_sidebar')) {
    register_sidebar ('custom');
    } ?>

    ‘custom’ would mean you have to create a custom sidebar-file under the name sidebar-custom.php. Sidebars get indexed. Your default sidebar (sidebar.php) gets indexed as 1. Every succeeding one will have an index higher than 1. You will see your sidebars now listed in ‘Apearance’ -> ‘Widgets’.

    In your sidebar-templates you may now call the dynamic generated content (widgets) by the index of your sidebars:

    < ?php if ( function_exists ( dynamic_sidebar(1) ) ) : ?>
    … regular html …
    < ?php dynamic_sidebar (1); ?>
    … regular html …
    < ?php endif; ?>

    You can integrate your sidebars in your template-files (e.g. index.php, single.php, archives.php):

    < ?php get_sidebar (); ?>

    gets you the default sidebar.

    < ?php get_sidebar ('custom'); ?>

    should display your custom sidebar.

    What I can’t figure out is where and how to put this code in my functions.php file. I am looking at the default functions.php for Kubrick. The first 8 lines are:

    < ?php
    if ( function_exists('register_sidebar') )
    'before_widget' => ‘
  • ‘,
    ‘after_widget’ => ‘
  • ‘,
    ‘before_title’ => ‘

    ‘after_title’ => ‘


Which doesn’t exactly match up the code from the codex!

This is then followed by a lot of code which seems to be relevant only to the function for modifying the kubrick header image and background colours, which I don’t use.

I am thinking I can just delete everything after line 8, but am hesitant to do so!

Anyone able to guide me through this? What a great learning experience! :D

# April 8, 2009 at 4:38 pm

Funnily enough I saw this article today and thought of you

# April 9, 2009 at 1:56 pm
"apostrophe" wrote:
Funnily enough I saw this article today and thought of you

Thanks!! This post and link are extremely useful. I have it working nicely locally and "hopefully" all will go well when I upload to my site.


# January 23, 2011 at 10:16 am

You could also simply use a plugin to create custom sidebars for each page or post. The Graceful Sidebar Plugin does this for you.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.