WordPress.org

Forums

More elegant way to display different sidebars on each page? (9 posts)

  1. Johnny2001
    Member
    Posted 9 years ago #

    Hi all,

    I'm in the mood to revamp my current site with a new theme, but first I'd like to get your input on how I've done things so far and see if there might be a better way to achieve the same end in WP.

    Right now my front page is the blog and everything else is static pages. On each page I have detailed content in the main section with a separate sidebar that contains supporting or supplemental info. The sidebar is different depending on each page. You can take a look at my site here http://www.johnhorneguitar.com and take a look at some of the pages to see what I'm up to.

    In order to achieve this, I have created a different template for each page and each page then calls a different sidebar. Of course each side bar has it's own template as well. So far, I've been storing all of my custom sidebar content as pages and using and using the "improved include page" plugin to fly in the content to the sidebar template. This is all okay by me because the info on the pages and sidebars remains fairly static.

    So my question is: is there an easier way to do this?

    If not, what I'm doing now works except for the fact that if I do re-theme the site I'll have to create several new template files and my list of templates is getting a bit long.

    Any suggestions or feedback welcome.
    John

    BTW using WP 2.0 (without any problems so far)

  2. kdharris
    Member
    Posted 9 years ago #

    There's a very elegant and very simple way to do this. I'm in the process of "upgrading" my blog, but that's another story. Point is, not all pages are complete, so the following coding doesn't completely show its possibilities.

    EDIT: Or like since it displayed funny even in between backticks, get the code at http://boison.org/wp-content/themes/simple/sidebar.txt.

    You can see this "in action" at boison.org. I think it's pretty self-explanatory, though I'd try to clarify wherever needed.

  3. Johnny2001
    Member
    Posted 9 years ago #

    hi kdharris,

    Very nice.

    My knowledge of php is pretty poor, mostly learned through dealing with WP, but this looks promising. From what I understand, the sidebar.txt file calls the sidebar depending on what the content page is - right?

    I'll definitely have to bone up on my php before I try this, though.

    Thanks for your help!

  4. kdharris
    Member
    Posted 9 years ago #

    Right, statements within sidebar.php (I only uploaded a text file so that you could view the code) display the various content depending on the page that's being viewed. For example, <?php /* Single */ if (is_single()) { $display_single = true;?> would display


    <?php if( $display_single ) { ?>
    <h1><?php _e('Information'); ?></h1>
    This is an archived entry published on <?php the_time('F jS, Y',display) ?>.
    Filed under <?php the_category(', ') ?>.
    You can follow any responses to this entry through the <?php comments_rss_link('RSS 2.0'); ?> feed.
    <?php } ?>
    ,

    only on pages that use the single page template, if that makes sense? http://boison.org/archives/2005/12/23/leaving-the-city/ is an example.

    To make sidebar content for different pages, just copy the else if statement <?php /* Colophon */ } elseif (is_page() && ( get_query_var('pagename') == 'colophon')) { $display_colophon = true;?>, replacing 'colophon' with the page slug and $display_colophon with whatever sections you want to display.

  5. judyschmitz
    Member
    Posted 9 years ago #

    1) question for jonny: HOW DID YOU CALL A DIFF. SIDEBAR into your template? I have created sep templates but cannot find the call to the sidebar to replace it with my sidebara.php page. Help!

    2) the above code assumes very definitive page structures, ie, only a single or a double page, and then the relative sidebar content. Yes?

    tks

  6. Johnny2001
    Member
    Posted 9 years ago #

    judyschmitz,

    I just used: <?php include 'Events_sidebar.php'; ?> to call the page I wanted. Of course, as I said before each side bar has it's own template too and thats when things start getting a bit bulky.

  7. Johnny2001
    Member
    Posted 9 years ago #

    kdharris

    Man, that is sweet.

    I'll give it a try later on and see is I can make it happen.

    The only reason I have different page templates is so I could include the code to call a different sidebars to each one.

    I'll touch base later and let you know if I had any success.

  8. judyschmitz
    Member
    Posted 9 years ago #

    Johnny: what a beautiful site! I'm going to sic my daughter on your wonderful 'name the note' note trainer...its wonderful! And a wonderful thing for kids (former songwriter here).

    Question: what theme did you use for this layout?

  9. Johnny2001
    Member
    Posted 9 years ago #

    kdharris

    one more question...

    "replacing... $display_colophon with whatever sections you want to display."

    does that mead I can use info from a post or a page here?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.