WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] sidebar - how to call a sidebar php? (10 posts)

  1. cenzi
    Member
    Posted 7 years ago #

    I have spent all morning trying to redo sidebars for different pages within my website (http://dev.naaw.net/wordpress)

    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???

  2. peiqinglong
    Member
    Posted 7 years ago #

    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');
      	else
      		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.
  3. cenzi
    Member
    Posted 7 years ago #

    so is the fact that you write: sidebarnews.php

    and then: sidebar_news()

    is that correct? because this is not working...

  4. cenzi
    Member
    Posted 7 years ago #

    I'm sorry, but does anyone have another method, because this one is NOT working for me :(

  5. tsguitar
    Member
    Posted 7 years ago #

    I have one sidebar.php that's filled with conditional tags. Those conditionals are what determine the content displayed in the sidebar:
    http://wordpress.pastebin.com/f2eaf36cb

    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:
    http://theundersigned.net/2006/03/wordpress-conditional-tags/

    And from the Codex:
    http://codex.wordpress.org/Conditional_Tags

    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?

  6. cenzi
    Member
    Posted 7 years ago #

    ref: Here's a good write up I just found:
    http://theundersigned.net/2006/03/wordpress-conditional-tags/

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

  7. tsguitar
    Member
    Posted 7 years ago #

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

  8. peiqinglong
    Member
    Posted 7 years ago #

    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');
    	else
    		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') )
    	register_sidebar(array(
    		'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:

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

    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.

  9. Eric Amundson
    Volunteer Moderator
    Posted 6 years ago #

    Thanks peiqinglong - I had the same question and you did a great job of answering it. :)

  10. ganges
    Member
    Posted 6 years ago #

    peiqinglong, thanx alot!

Topic Closed

This topic has been closed to new replies.

About this Topic