Custom Page Template to show different sidebar
-
Hey everyone, been trying to create a custom page template that when selected will show a different sidebar.
The first step I did was edit my functions.php file to have one sidebar called “sidebar advice” and another called “sidebar projectors (which does show up in Presentation-Widgets) and it is as follows:
<?php
if ( function_exists(‘register_sidebar’) )
register_sidebar(array(‘name’=>’sidebar advice’,
‘before_widget’ => ‘<li id=”%1$s” class=”widget %2$s”>’,
‘after_widget’ => ”,
‘before_title’ => ”,
‘after_title’ => ”,
));
register_sidebar(array(‘name’=>’page projectors’,
‘before_widget’ => ‘<li id=”%1$s” class=”widget %2$s”>’,
‘after_widget’ => ”,
‘before_title’ => ”,
‘after_title’ => ”,
));I then made a template which is a copy/paste of my original page.php file, only changes I did here was add the Template Name: page advice to the start, and at the bottom I changed the get sidebar to <?php include (TEMPLATEPATH . ‘/sidebar_advice.php’); ?>. I saved this file as page advice.php and uploaded onto my site. The code for this page is as follows:
<?php
/*
Template Name: page advice
*/
?>
<?php get_header(); ?><div id=”content” class=”narrowcolumn”>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>”>
<h2><?php the_title(); ?></h2>
<div class=”entry”>
<?php the_content(‘<p class=”serif”>Read the rest of this page »</p>’); ?><?php wp_link_pages(array(‘before’ => ‘<p>Pages: ‘, ‘after’ => ‘</p>’, ‘next_or_number’ => ‘number’)); ?>
</div>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link(‘Edit this entry.’, ‘<p>’, ‘</p>’); ?>
</div><?php include (TEMPLATEPATH . ‘/sidebar_advice.php’); ?>
<?php get_footer(); ?>
The last thing I did was then to copy my original sidebar.php file and create one called sidebar_advice.php. I did not make any changes to this file, it is basically an exact duplicate of my original file.
(I repeated the same steps for the second sidebar “sidebar projectors, created a template called page projectors.php and a sidebar called sidebar_projectors.php).
ISSUE: When I go to my site the sidebar_advice loads up all through out my site even if I write a page and select the sidebar_projectors template. What am I missing?
- The topic ‘Custom Page Template to show different sidebar’ is closed to new replies.