WordPress.org

Ready to get started?Download WordPress

Forums

Sidebar depending on URL/subpages (2 posts)

  1. tomleadbetter
    Member
    Posted 5 years ago #

    Hi.

    I have this code that works to some extent:

    <?php
    
    if ( is_page('about')  || $post->post_parent == '2') {
        get_sidebar_about();
    }
    
    elseif ( is_page('somethingelse')  || $post->post_parent == '155') {
        get_sidebar_somethingelse();
    }
    
    elseif ( is_page('careers')  || $post->post_parent == '160') {
        get_sidebar_careers();
    }
    
    else {
        get_sidebar();
    }	
    
    ?>

    So if I have http://www.mydomain.com/about, it loads the sidebar I want.

    If I have http://www.mydomain.com/about/page1, it also works fine.

    However, if I have http://www.mydomain.com/about/page1/another-page, it loads the normal sidebar.

    So I need to check for any subpages/children of the url.

    Any help? Thanks

  2. Michael Fields
    Themer
    Posted 5 years ago #

    Something like this would work:

    $ancestors = get_post_ancestors( $post->ID );
    $root_parent = 15;
    
    if( in_array( $root_parent, $ancestors ) )
    	# DO STUFF

Topic Closed

This topic has been closed to new replies.

About this Topic