treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Page aware sidebar.php in Wordpress.

  • Hi

    I'm trying to make my custom sidebar.php file for wordpress aware of what page it is beeing loaded on. You see, I want to add "sub links" when you click a category link so to speak. At this time the sidebar is made with normal links like this:

    <h3><a href=\"index.php\">Home</a></h3><br>
    <h3><a href=\"?page_id=373\">Shop</a></h3><br>
    <h3><a href=\"?page_id=2\">About us</a></h3><br>
    <h3><a href=\"?page_id=237\">Contact us</a></h3>


    Anyone know an easy way to do this?


    Knut.
  • Why don't you use wp_list_pages http://codex.wordpress.org/wp_list_pages
  • I might be misunderstanding your question, it sounds like a job for Conditional statements, however....

    You can easily manage this with widgets!

    I use flexipages to set the parent / child links, you can have multiple instances of flexipage if you have more than one set of subpages. Then I use TS custom Widgets to control which widgets appear on which pages / posts / categories / archives etc. using conditional statements.

    For me this was great because it eliminated the need to create custom templates calling custom sidebars etc. I found that to get messy in a hurry and difficult to manage changes.

    Cheers!
    Chris
    http://www.krowchukdressage.com
  • I ran into a similiar problem with one of my wordpress sites.

    I ended up using this code to display subpages:

    <?
    if (!is_category() && !is_archive() ) {
    if( $post->post_parent != 0 ){
    // display subpages of my parent page
    ?>
    <div class=\"widget\"><h3><span><a href=\"<?php echo get_permalink($post->post_parent) ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php echo get_the_title($post->post_parent); ?>\"><?php echo get_the_title($post->post_parent); ?></a></span></h3>
    <ul>
    <?
    $children = wp_list_pages('title_li=&echo=0&child_of=' . $post->post_parent);
    if ($children) echo $children;
    ?>
    </ul>
    </div>
    <?
    }else{
    // display subpages of myself
    ?>
    <div class=\"widget\"><h3><span><a href=\"<?php echo get_permalink($post->ID) ?>\" rel=\"bookmark\" title=\"Permanent Link to <?php echo get_the_title($post->ID); ?>\"><?php echo get_the_title($post->ID); ?></a></span></h3>
    <ul>
    <?
    $children = wp_list_pages('title_li=&echo=0&child_of=' . $post->ID);
    if ($children) echo $children;
    ?>
    </ul>
    </div>
    <?
    }
    }
    ?>


    And it works well, if the page is a subpage, or has subpages then they get displayed. This can be changed how ever someone else wants to use it. But it's worked well on a couple of wordpress sites now.