Display a specific dynamic sidebar widgets on a specific page

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.

Read More

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.

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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.