1

I have had a wordpress site handed off to me halfway through completion. It's been at least a year since I last used wordpress and there are a lot of things I'm finding different. The way I am used to displaying different content on different pages is to create page templates for each page that needs unique content.

I am trying to display a different sidebar on one particular page than on the rest of the site. What I would have done a year ago is make a page template for this page and make a new sidebar file and then include the sidebar file in the page template. I've found that this doesn't work. The main culprits I can see are loop.php & loop-page.php, which were non-existent in the stripped down theme that I would typically use when building a site long ago. However, it appears that whoever had their hands on this thing before me has copied the 'twenty ten' theme and made changes to it under a new name.

I've registered a new dynamic sidebar in the theme's functions.php and I can see it in the Widgets section of the Dashboard and add widgets to it.

I can't for the love of god get it to display on the page I want it to though. I've tried the following:

1.) Given the page a custom template template_news.php and placed the following where the sidebar should be called:

include ('sidebar-news.php');

The sidebar-news.php filed containing code I copied from sidebar.php with the "News" sidebar id that I listed in functions.php:

<?php if ( is_active_sidebar( 'sidebar-news' ) ) : ?>

<div id="secondary" class="widget-area" role="complementary">
    <ul class="xoxo">
        <?php dynamic_sidebar( 'sidebar-news' ); ?>
    </ul>
</div>

<?php endif; ?>

The page template is applied to the correct page.

2.) Gone into page.php and changed:

<?php get_sidebar(); ?>

to:

<?php 

if(is_page('18')) {
   include ('sidebar-news.php');
}
else {              
   get_sidebar(); 
} ?>

I've also checked several times to make sure the page ID is correct and tried using the slug name instead but to no avail.

3.) Tried the previous in loop-page.php

4.) Placed the following conditional statement in sidebar.php:

<?php
if ( is_active_sidebar( 'sidebar-news' ) && is_page('18') ) : ?>
    <div id="sidebarnews" class="widget-area" role="complementary">
    <ul class="xoxo">
    <?php dynamic_sidebar( 'sidebar-news' ); ?>
    </ul>
    </div>
<?php endif; ?>

Also after doing some research and finding out that the proper way to extend or change the functionality of a theme is to create a child theme, I attempted to do that, but it caused some things to break that I don't have the time to try and fix.

I've found that the default 'sidebar.php` is being called and displayed on the page, however, even though my conditional statement with the page ID is present, it's not displaying the custom sidebar widgets.

I also cannot seem to find how the default sidebar is being called on the page because removing get_sidebar(); altogether on my custom template template_news.php , page.php , or loop-page.php doesn't seem to do anything.

Please can someone let me in on how to add this sidebar to the page.

1

2 Answers 2

3

I think what you might need is simply to run register_sidebar() in an 'widgets_init' hook. The TwentyTen theme has examples for register_sidebar() in its functions.php file, but here's what it might look like:

add_action( 'widgets_init', 'twentyten_widgets_init' );
function twentyten_widgets_init() {
  register_sidebar( array(
    'name' => __( 'News Sidebar', 'yoursite' ),
    'id' => 'sidebar-news',
    'description' => __( 'The News Sidebar Area', 'yoursite' ),
    'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
  ));

  /*
   * THE REST OF THE SIDEBAR REGISTRATION CODE FROM twentyten_widgets_init() GOES HERE
   */
}

Also, in your template_news.php page template use the following code to call your sidebar which will load the sidebar-news.php file you created above:

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

You shouldn't need any of the other things you tried, and definitely not any include statements.

6
  • I have already done this. It was the first thing I tried exactly as you have laid out. However it does not work. The code in my custom template for the content and the sidebar are being overridden by default settings in loop.php & loop-page.php.
    – biggles
    Commented Sep 16, 2011 at 15:03
  • 1
    @jlego - Hmm. I tested my original with TwentyEleven and just retested with TwentyTen and it works. loop.php & loop-page.php don't have anything to do with sidebar generation, unless of course the other developer did some really screwy things. Can you update your question and post those two files as well as the content of your template_news.php file? Commented Sep 16, 2011 at 15:27
  • 1
    @jlego - Also, if you don't have PhpStorm + Zend Debugger in your toolset, you might consider getting a copy. With it you could trace through your site line-by-line and see exactly why it is or is not working. Commented Sep 16, 2011 at 15:32
  • It was really frustrating me because I knew that this should work, and I had checked all the variables to make sure I had the template set correctly and didn't have any mistakes in my code. Turns out that whoever had their hands on it before me had gotten a blogroll to show up on the page by setting that particular page to the "posts" page in the wp admin. So when I went to make a custom template, that setting was overriding my template and telling the page to run the code in those files. Thanks for your help though. I will check out PHPStorm and Zend Debugger.
    – biggles
    Commented Sep 17, 2011 at 3:38
  • Even though this did not solve the specific issue I was having, This is the correct way to accomplish adding a sidebar to the page. I believe the actual solution to my issue was limited to my specific case, so I am marking your answer as the solution to this question in response to the number of views this question has.
    – biggles
    Commented Sep 3, 2013 at 21:49
1

To display a widget only on specific pages where all your pages have the same sidebar, you may use one of those plugins :

Display Widgets

Simply hide widgets on specified pages. Adds checkboxes to each widget to either show or hide it on every site page.

Widget Logic

Widget Logic lets you control on which pages widgets appear using WP's conditional tags. It also adds a 'widget_content' filter.

1

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.