Results 1 to 5 of 5

Thread: Displaying different sidebars depending on main page

  1. #1
    Karl1's Avatar
    Karl1 is offline Hello World
    Join Date
    Jul 2011
    Location
    Seville, Spain
    Posts
    35

    Default Displaying different sidebars depending on main page

    Hi guys,
    I need to display a different sidebar depending on the page or child of, so I have
    PHP Code:
    <?php      if (is_page('405'))        { get_sidebar('2');}    elseif ( is_page('98') )        {get_sidebar('3');}    else         { get_sidebar(); }         ?>
    but I can't figure out how to make it calls the same sidebar if we are in a sub-page

    Thanks for any help
    Discovering Wordpress

  2. #2
    dgwyer's Avatar
    dgwyer is offline Tavern Regular
    Join Date
    Jun 2010
    Location
    London, UK
    Posts
    230

    Default

    Most people use the Widget Logic Plugin for this rather than hard coding it.

  3. #3
    Jeffro's Avatar
    Jeffro is offline WPTavern Forum Admin
    Join Date
    Jan 2009
    Location
    Ohio
    Posts
    2,359

    Default

    What's the purpose in coding all those different sidebars? Is it to show specific widgets? If that's the case, using widget logic would make your life a lot easier as you can do the same thing but with just one sidebar.

  4. #4
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    For a single-depth child page, try:
    PHP Code:
    <?php
    // Globalize $post variable
    global $post;
    // If Parent Post is 98
    if ( '98' == $post->post_parent ) {
        
    // This is a sub-page of Page ID 98;
        // Do something; e.g. output the sidebar
        
    get_sidebar('3');
    }
    For greater hierarchical depths, you'll need to use something different, such as get_posts() or get_pages() to retrieve the list of child-page IDs, and match them that way.
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  5. #5
    Karl1's Avatar
    Karl1 is offline Hello World
    Join Date
    Jul 2011
    Location
    Seville, Spain
    Posts
    35

    Default

    I found Dynamic widgets before Widget logic, but I see that widget logic has been used more often, I guess I'll try it out too.
    Anyone knows the differences?

    @chipbennet
    Thanks, I guess I'll use one of this plugins as otherwise I'll have to create 5 or 6 sidebars (files)
    Discovering Wordpress

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •