Support » Themes and Templates » sidebar – how to call a sidebar php?

  • Resolved cenzi


    I have spent all morning trying to redo sidebars for different pages within my website (

    I have managed to get ALOT done by reading the support forums, but there is ONE problem.

    I have figured that within my archive.php I can call a NEW sidebar that works fine with all my posts. So thats okay, each time a new post comes in, I will have a new sidebar which works perfectlty fine.

    But now, I have a few PAGES and I want each PAGE to have another sidebar of its own. I thought I could call them by using the following: (keep in mind my newsidebar is called sidebarabs)

    <?php get_sidebarabs(); ?>

    I used:
    <?php include ('sidebar_news.php'); ?>
    within my archive.php and it worked fine.

    My problem is that I KNOW NOT WHERE to put this new <?php get_sidebarabs(); ?>; because it is not a particular archive.php or category.php pr anything…. can I not pull it out throught the editor???

Viewing 9 replies - 1 through 9 (of 9 total)
  • Actually that is somewhat of a roundabout way to do that. This is what you do instead:

    1. Create a file called functions.php and put it into your theme directory
    2. Inside that functions.php file, put:
      function get_sidebar_news() {
      	do_action( 'sidebar_news' );
      	if ( file_exists( TEMPLATEPATH . '/sidebarnews.php') )
      		load_template( TEMPLATEPATH . '/sidebarnews.php');
      		load_template( ABSPATH . 'wp-content/themes/default/header.php'); }
    3. Create a filed called sidebarnews.php and put whatever you want in it.
    4. In whatever page/section you want to use this new sidebar, call it via `<?php get_sidebar_news() ?>.
    5. You can create as many sidebar functions as you want doing the same method.

    so is the fact that you write: sidebarnews.php

    and then: sidebar_news()

    is that correct? because this is not working…

    I’m sorry, but does anyone have another method, because this one is NOT working for me 🙁

    I have one sidebar.php that’s filled with conditional tags. Those conditionals are what determine the content displayed in the sidebar:

    You could just use is_page() to determine what is displayed in the sidebar when readers view a Page.

    Here’s a good write up I just found:

    And from the Codex:

    This would give you a single sidebar file that you’d call on in every template, but that would display different information depending on what type of page it is. Get it? Did I get your question right?

    ref: Here’s a good write up I just found:

    that was EXACTLY what I was looking for. Thank you SO much. Thanks to the other guy too, for trying.. maybe I just didnt get it, but THIS one is perfect…

    Can you mark this “Resolved” if that fixed your trouble? Thanks.

    Sorry, I must not have been clear enough and I skipped a few steps.

    WordPress by default defines certain functions that call certain files (e.g. header.php is called when <?php get_header() ?> is used and sidebar.php is called when <?php get_sidebar() ?> is used). By creating a functions.php file inside your theme directory, you can define additional functions that pertains only to your theme without hacking WP core files making it very easy to upgrade and keep your customized changes.

    So in the functions.php file, if you want a second sidebar, you have to define a new function, in this case: sidebar_news(). And everytime you use <?php get_sidebar_news() ?>, it will run anything inside sidebarnews.php file.

    So inside the functions.php file, you would put:

    function get_sidebar_news() {
    	do_action( 'sidebar_news' );
    	if ( file_exists( TEMPLATEPATH . '/sidebarnews.php') )
    		load_template( TEMPLATEPATH . '/sidebarnews.php');
    		load_template( ABSPATH . 'wp-content/themes/default/header.php'); }

    that defines the get_sidebar_news() function. But we need to register it so that the widgets will work, so you have to add this also:

    if ( function_exists('register_sidebar') )
    		'name' => 'sidebar_news',
    		'before_widget' => '<li>', // Removes <li>
    		'after_widget' => '</li>', // Removes </li>
    		'before_title' => '<h3>',
    		'after_title' => '</h3>',

    So that when you go under Presentation > Widgets, you’ll have a new sidebar section. Now inside of the sidebarnews.php file, you need to tell it do something:

          <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar('sidebar_news') ) : else : ?>
          <?php endif; ?>

    So now that if there are widgets enabled, then it will display widgets and if not, nothing is displayed.

    That should give you a new function that you can use to call the sidebarnews.php file and allow you to add widgets via the Presentation > Widgets. This only works if the theme has already been widgetized.

    Eric Amundson


    Volunteer Moderator

    Thanks peiqinglong – I had the same question and you did a great job of answering it. 🙂

    peiqinglong, thanx alot!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘sidebar – how to call a sidebar php?’ is closed to new replies.