treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Wordpress custom template, custom sidebar?

  • 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,
    Chris
  • 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'); ?>
  • 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?

    Cheers!
    Chris
  • 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.
  • Hey,

    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 http://codex.wordpress.org/Customizing_Your_Sidebar 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') )
    register_sidebar(array(
    'before_widget' => '<li id=\"%1$s\" class=\"widget %2$s\">',
    'after_widget' => '</li>',
    'before_title' => '<h2 class=\"widgettitle\">',
    'after_title' => '</h2>',
    ));


    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
  • Funnily enough I saw this article today and thought of you http://wpcandy.com/articles/tutorials/how-to-make-the-most-out-of-your-sidebar.html
  • "apostrophe" said:
    Funnily enough I saw this article today and thought of you http://wpcandy.com/articles/tutorials/how-to-make-the-most-out-of-your-sidebar.html


    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.

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