Forums

How to display different sidebar on different pages ? (23 posts)

  1. angelro
    Member
    Posted 4 years ago #

    Hello guys. I want do display different sidebars on my wordpress site. For example I don't certain things to appear on the contact page, on the terms page etc. How can I do that please ?

    I saw that for homepage you can use something like this :
    <?php if(is_home()) do something here ?>

    How can I use this for the other pages ?

    Looking for your answers.
    Raz

  2. Justin Tadlock
    Member
    Posted 4 years ago #

    This will explain everything you'll ever need to know about using conditional tags such as is_home() for your WP blog:
    http://codex.wordpress.org/Conditional_Tags

    You could essentially make a sidebar for every single post, page, and archive on your site.

  3. jdawg2k
    Member
    Posted 4 years ago #

    Hi Angelro, I'm a big time WordPress Newb but found an answer to this one quickly. For every sidebar you want, add the following:

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

    Where 'simple' refers to a page name I have in the same folder called:

    sidebar-simple.php

    Hope that helps.

  4. moshu
    Member
    Posted 4 years ago #

    Ah, that's a new feature (and well-kept secret!) in 2.5:
    Include_Tags#The_Sidebar_Template
    Thanks for bringing it to our attention!

  5. Justin Tadlock
    Member
    Posted 4 years ago #

    If you're just using widgets, then just using conditional tags within that one sidebar file will be sufficient.

    If you're coding each of your own sidebars, then you'll definitely want to take advantage of the new sidebar feature.

  6. moshu
    Member
    Posted 4 years ago #

    Re: widgets

    Aren't the widgets overwriting whatever code you have in your sidebar.php code?

  7. Justin Tadlock
    Member
    Posted 4 years ago #

    No, the widgets don't overwrite everything. Only what I tell it to. Here's a snippet from one of my themes.

    This sets a variable for which widgets I want to use depending on the type of page I'm on:

    // Sidebars
    	if(is_front_page())
    		$op_sidebar_id = __('Sidebar Home','options');
    // Single page and post type sidebars
    	elseif(is_attachment())
    		$op_sidebar_id = __('Sidebar Attachment','options');
    	elseif(is_single())
    		$op_sidebar_id = __('Sidebar Single','options');
    	elseif(is_page())
    		$op_sidebar_id = __('Sidebar Page','options');
    	else
    		$op_sidebar_id = __('Sidebar Home','options');

    After that, I add the normal widgetized section with the variable set:

    // If using the No Sidebar template
    	if(is_page_template('no-sidebar.php')) :
    		echo '<!-- User is using the "No Sidebar" page template. -->';
    // Else, show the sidebar
    	else :
    		echo "<div id='sidebar' class='$op_sidebar_pos'>";
    		if(function_exists('dynamic_sidebar') && dynamic_sidebar($op_sidebar_id)) :
    		else :
    		// Default to Home page sidebar if no widgets are set
    			if(function_exists('dynamic_sidebar') && dynamic_sidebar(__('Sidebar Home','options'))) :
    			else : _e('Add content to your sidebar through the widget control panel.','options');
    			endif;
    		endif;
    		echo '</div>';
    	endif;

    [Edit]
    Of course, you'd have to set up all the widgets in functions.php or something. I just use an array and foreach loop to get all those on the widget panel.

  8. moshu
    Member
    Posted 4 years ago #

    :)
    I know that. However, most themes are coded in that way that in the sidebar.php everything is inside the 'dynamic_sidebar' stuff... and that's why we have all those posts around here saying "I edited my sidebar.php but the changes don't apply..."

  9. Justin Tadlock
    Member
    Posted 4 years ago #

    Yeah, you're right. That's a limitation of "most themes." I sometimes forget this. ;)

    I guess at this point that we need to see what angelro wants out of his or her sidebars. If it's just a couple of extra sidebar options, then we shouldn't get too complicated with this.

  10. angelro
    Member
    Posted 4 years ago #

    Hello guys. Tks for your help. So the site I am trying to change is oaklandhotelsinfo.com - to mention that the version I use is 2.0.5.

    Based on greenshady's advice I tried to do something like :

    On the http://www.oaklandhotelsinfo.com/best-western-park-plaza/ page I wanted to insert something else in the page. So in the sidebar I added something like :

    <?php

    if (is_page(7)) {
    <b>Site Name Here</b>
    } else {
    // This is not a subpage
    }
    ?>

    To mention that 7 is the id of the above page. But when I put this online I get something like : "Parse error: syntax error, unexpected '<' in"

    Can you help me out please ? What syntax should I use in my sidebar in order to achieve what I mentioned above ?

    Looking forward to hearing from you !

  11. Justin Tadlock
    Member
    Posted 4 years ago #

    The problem is that you're inserting HTML into PHP without closing off the PHP.

    <b>Site Name Here</b>

    Make sure you close the ?> tag before HTML and reopen <?php after.

  12. angelro
    Member
    Posted 4 years ago #

    Hello Greenshady. Tks for your reply mate. But I really cannot understand it. Can you please write the whole code for my example ? So I can copy/paste it in my wordpress and see if it works ? I would really appreciate that.

  13. DaveHS
    Member
    Posted 3 years ago #

    Hello all;

    Sorry in advance if this sounds either repetative or uninformed, but I too am a WP newbie, and it seems multiple issues are being discussed above, and having just gotten used to being able to work with "different sidebars on different pages" in an older version of WP, I am now totally lost! I can't even find what sub folder the pages are actually stored in! If someone could provide a "laymen" explanation, I would be so ever indebted! :-p

    For ease of explanation, I am working on http://firefoxnewsonline.net .

    Basically, what I want to do is have each "page" (aside from the "home" page) display a different sidebar (ie: sidebar_george.php, sidebar_dave.php, sidebar_library.php, sidebar_contact.php, etc). That said, i think I've figured out I have to add a "elseif" php script, though I am not sure exactly how to word it, and more importantly, which ".php" I need to add it to (page.php, template.php, etc), etc.

    But based on what I read above, I guess I now have a 2nd question, once I get the 1st one resolved of course! Some of the pages I want to have different sidebars for will also have different widgets. For example may have a polling widget, whereas another will have a youtube widget. Is this possible, and if so, what text do I have to add to what page?

    Again, sorry for my "layman" lameness, but struggling to figure this out! Thanks in advance for any assistance anyone provides.

    Respects
    Dave

  14. moshu
    Member
    Posted 3 years ago #

    I can't even find what sub folder the pages are actually stored in!
    Wrong question.
    Nothing is stored in any subfolder or files.
    Perhaps, your should learn first what Pages are in WordPress and after that we can talk about sidebars. While on the Codex page I linked to... read also about Page Templates, before you come back with the next question.

  15. DaveHS
    Member
    Posted 3 years ago #

    Thanks Moshu.

    I read that, and I was aware of all of it, and what I was referring to was the difference between the older version of WP I was using (I don't recall which one it was) & this one. I actually enjoy the fact that I can modify my pages now directly in the admin panel, but the point I was trying to make is where to go to modify each to display it's own sideabar & more importantly, what exactly to write (as everything I know, most being HTML & XML, has been self taught, and I am not totally fluent in PHP yet).

    My questions remain the same, what to write, and which file to modify. Thanks

  16. DaveHS
    Member
    Posted 3 years ago #

    Ok, after a little more digging, I was finally able to find the solutions to my 1st set of questions, that being what would be the format of the PHP scripting required to get each page to have it's own side bar, & what *.php document to modify it in.

    Q1: Thanks to someone else reporting the issue (Custom sidebar per page), I was able to figure out that this is how part of the scripting would look in my case:

    } elseif (is_page()) {
            // we're looking at a static page.  Which one?
            if (is_page('About')) {
                 // our about page.
                 include(TEMPLATEPATH . '/sidebar_about.php');
            } elseif (is_page('3')) {
                 include(TEMPLATEPATH . '/sidebar_news.php');
            } elseif (is_page('5')) {
                 include(TEMPLATEPATH . '/sidebar_george.php');
            } elseif (is_page('6')) {
                 include(TEMPLATEPATH . '/sidebar_dave.php');
            } elseif (is_page('8')) {
                 include(TEMPLATEPATH . '/sidebar_library.php');
            } elseif (is_page('10')) {
                 include(TEMPLATEPATH . '/sidebar_links.php');
            } elseif (is_page('7')) {
                 include(TEMPLATEPATH . '/sidebar_contact.php');
            } else {
                  // catch-all for other pages
                 include(TEMPLATEPATH . '/sidebar.php');        }

    Q2: As to what document to modify, it is the actual file acting as the primary "sidebar.php", as dictated by what is in "page.php"

    Now, however, my 2nd set of questions regarding widgets come into play.

    When in the "Design/Widgets" site admin screen, the "Current Widgets" box on the right hand side of the page does not list all the sidebar php pages I have uploaded in my theme directory. How do I get all my other uploaded sidebar files such as "sidebar_george.php", "sidebar_dave.php", etc, to be added to that list?

    Inching my way forward! Sorry for my newbie-ness! I love WP 2.6, but it's taking a lot of getting used to! :-p

  17. moshu
    Member
    Posted 3 years ago #

    Try a search for multiple sidebars and widgets.

  18. DaveHS
    Member
    Posted 3 years ago #

    Thanks Moshu, tried to search that but to no avail.

    Most posts discuss adding the same widget to multiple sidebars. The problem in my case, however, is that my "Current Widgets" drop down box is missing 6 of the 8 sidebars I have uploaded to the server.

    Anyone else have any ideas on how to get the missing sidebar php's to show up in the "current widgets" drop down box?

    Thanks, Dave

  19. moshu
    Member
    Posted 3 years ago #

  20. DaveHS
    Member
    Posted 3 years ago #

    LOL! Talk about timing!

    Thanks Moshu, I found the same info, only in a different spot (Widgetizing Themes)! I appreciate you "trying to teach me to fish instead of just feeding me fish", however the only problem is I am doing all this during my lunch times at work on the overnight shift!

    For anyone else visting this page who is having this problem (uploaded sidebars not appearing in the "Current Widgets" drop down box), the short answer is you have to modify the "functions.php" file, and add php scripting manually to "register" the other sidebar php files.

    In my case, here is what it looks like:

    <?php
    if ( function_exists('register_sidebars') );
    
    register_sidebar(array('name'=>'sidebar',));
    register_sidebar(array('name'=>'sidebar_about',));
    register_sidebar(array('name'=>'sidebar_news',));
    register_sidebar(array('name'=>'sidebar_george',));
    register_sidebar(array('name'=>'sidebar_dave',));
    register_sidebar(array('name'=>'sidebar_library',));
    register_sidebar(array('name'=>'sidebar_links',));
    register_sidebar(array('name'=>'sidebar_contact',)); 
    
    ?>

    But in the end, again, thank you Moshu for your assistance! :-p
    Respects, Dave

  21. tim@24medium.com
    Member
    Posted 3 years ago #

    DaveHS

    So I can see the sidebars in my widgets section, now how do I get them to show up with the changes on the actual pages?

    Sorry still new at this....been searching and trying for ever.

  22. alanft
    Member
    Posted 3 years ago #

    in case anyone prefers it, my plugin

    http://wordpress.org/extend/plugins/widget-logic/

    lets you specify when widgets appear in the sidebar. in the widget admin interface you can specify WP conditional tags and so on. it's sometimes easier than writing your own sidebar php theme file

  23. oceanofp
    Member
    Posted 3 years ago #

    I have two sidebars and for certain pages I would like to drop the left sidebar and have only a list of pages on the sidebar for a particular page (pages widget is on left sidebar right now).

    I have a leftbar.php and a sidebar.php. Would the methods and codes work for what I want?

    At the moment I'm using wp 2.6.2 (waiting on my server for 2.6.3 upgrade) with the Darkwater-11 theme templates.

    How the page.php calls the sidebars:

    <?php include (TEMPLATEPATH . '/leftbar.php'); ?>

    <?php get_sidebar(); ?>

    If I'm able to omit the left bar on certain pages, how would I code it to display only the list of pages on the sidebar of a particular page?

Topic Closed

This topic has been closed to new replies.

About this Topic