• kate3686

    (@kate3686)


    I am in the process of adding multiple sidebars to the Studio Theme and I’m running into an issue, which is really due to my ignorance of PHP. OK…this is what I want to do. I want to be able to choose different page templates under Pages, and those templates each call a different sidebar.

    Here’s what I’ve done: I’ve added two new sidebars to the array and I can see them under Widgets. I’ve also added two new page templates, which I am able to select for my pages. All is well there, BUT…

    I cannot get the templates to load the correct sidebars. I’m 100% sure that I’m just not writing the code correctly. I’m sure I’m putting in the right file. I’ve tried writing it different ways and no luck. This is what I have:

    <?php if(is_page_template('page-sidebar1.php')) get_sidebar('secondary_sidebar1');
    elseif(is_page_template('page-sidebar2.php')) get_sidebar('secondary_sidebar2');
    elseif(is_page_template('page-fullwidth.php') ;
    else get_sidebar('main_sidebar');
    ?>

    So I want sidebar1 and sidebar 2 to load with the matching page templates. I want the fullwidth template to NOT load a sidebar. And I want any other page template to load the main sidebar.

    Please, please help!! Thanks in advance!

Viewing 8 replies - 1 through 8 (of 8 total)
  • In what file did you put the code that you posted? The “where” is probably most of your problem.

    Based on what you’re describing, you don’t need the if statements at all. Because the pages are using specific templates, you already know whether it’s being used or not. So yo would put the functions for the sidebar directly in your page template at the appropriate place, and then call the sidebar that you want to call. When it comes to your full-width page, then don’t put any sidebar at all, and make sure to adjust your CSS to widen the content. And then you have a general template for all the other pages that calls the main sidebar.

    Does that make sense?

    Thread Starter kate3686

    (@kate3686)

    Unfortunately, this theme’s page templates all just have this code:

    <?php
    	get_header();
    	get_template_part('library/template_posts');
    	get_footer();
    ?>

    So, I am putting the code I pasted above in the template_posts.php file. I would like to keep it that way so I’m keeping with the way the theme was created. I suppose I could just change each page template’s php file. But I’m sure there’s a way to make it work in the template_posts.php file.

    Does that make sense? Thanks for responding, I appreciate the help!

    Find wherever the sidebar code is in the template_posts file, and then you want to use the is_page() function instead of is_page_template() function. It would look something like this:

    <?php
    if ( is_page( /*page ID, slug, etc. goes here*/ ) ) {
       get_sidebar( 'secondary_sidebar1' ); }
    elseif ( is_page( /* page ID, slug, etc. */ ) ) {
       get_sidebar( 'secondary_sidebar2' ); }
    else {
       get_sidebar( 'main_sidebar' ); }
    ?>

    I would recommend finding some way to test for fullwidth setting first, and then running the code above for every page that is not fullwidth.

    Thread Starter kate3686

    (@kate3686)

    Thank you for continuing to help. I’ve put in this code:

    <?php
    if is_page( 'page-sidebar1.php' ) {
       get_sidebar( 'secondary_sidebar1' ); }
    elseif is_page( 'page-sidebar2.php' ) {
       get_sidebar( 'secondary_sidebar2' ); }
    else {
       get_sidebar( 'main_sidebar' ); }
    ?>

    And get this error: “Parse error: syntax error, unexpected T_STRING…”

    Are you suggesting that I list different pages here instead of the template? My goal is to be able to change templates on the page and have the sidebar change. So, I’d like this code to be contingent on the template, not the specific page. (On another site, I’ve used a theme that works this way and know it’s possible.) I just don’t know the right PHP :o(

    Sorry, you’re right… I was still thinking of using individual templates. You should be able to use is_page_template().

    For that “T_STRING” error, post the exact error message, and use the WordPress pastebin to post your entire template file. That should make it easier to figure out what’s going wrong.

    I may not be able to look at this much more tonight (as it’s getting late in my timezone), but there are of course plenty of others here in these forums, and I can take a look tomorrow if someone else hasn’t helped you by then.

    Thread Starter kate3686

    (@kate3686)

    Thank you for your willingness to help. If you can help tomorrow, I would appreciate it! So here’s where I am… I’ve declared the page templates at the top of the template_posts.php file to be more consistent with the rest of the code and am no longer getting the T_STRING error (whew). But, when I test different templates on pages, well, they all just display the main sidebar and the only the main sidebar :o/ So I’m definitely doing something wrong.

    Here’s the complete template_posts code (in pastebin, as requested):
    http://pastebin.com/u/kate3686

    I also put in there the sidebar_config file just in case there’s some issue there. As I said already, the different page templates all have this code (and just have been given different names):

    <?php
    	get_header();
    	get_template_part('library/template_posts');
    	get_footer();
    ?>

    I am currently using the default, fullwidth, sidebar1, and sidebar2 templates in the site. The other templates mentioned in the template_posts code just came with the theme.

    I hope this info helps. Thank you, thank you, thank you for your help! :o)

    I made some tweaks to your code. A lot of changes were just for easier readability, but I also modified how you were setting up and calling the sidebars. Try this code and see if it works any better for you: http://pastebin.com/BE5M2k8c.

    Thread Starter kate3686

    (@kate3686)

    Still not working :o/ I guess I’m going to give up working within the template_posts file and put that code within the page template files.

    Thanks again for working with me on this!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Adding Multiple Sidebars to Studio Theme’ is closed to new replies.