FriendlyWP
11/22/2013 - 11:45 PM

Add simple section navigation (child/parent hierarchy) via a shortcode.

Add simple section navigation (child/parent hierarchy) via a shortcode.

// DISPLAY SIMPLE SECTION NAVIGATION VIA SHORTCODE
// adapted from the fantastic Simple Section Navigation plugin which is alas no longer being maintained
// http://wordpress.org/plugins/simple-section-navigation/
add_shortcode('simplesidenav', 'mm_simplesidenav');
function mm_simplesidenav( $atts, $content = null) {
    extract( shortcode_atts( array(
      'exclude'      => '',
      'topic'           => '',
      //'hierarchical'    => 'false',
      ), $atts ) );
    
    global $post;

    $exclude_list = $exclude;
    $excluded = explode(',', $exclude_list); //convert list of excluded pages to array 
    if ( in_array($post->ID,$excluded) ) return false; //if on excluded page, and setup to hide on excluded pages 
    
    $post_ancestors = ( isset($post->ancestors) ) ? $post->ancestors : get_post_ancestors($post); //get the current page's ancestors either from existing value or by executing function
    $top_page = $post_ancestors ? end($post_ancestors) : $post->ID; //get the top page id
    
    $thedepth = 0; //initialize default variables
     
    $ancestors_me = implode( ',', $post_ancestors ) . ',' . $post->ID;
    
    //exclude pages not in direct hierarchy
    foreach ($post_ancestors as $anc_id) 
    {
      if ( in_array($anc_id,$excluded) && $instance['hide_on_excluded'] ) return false; //if ancestor excluded, and hide on excluded, leave
      
      $pageset = get_pages(array( 'child_of' => $anc_id, 'parent' => $anc_id, 'exclude' => $ancestors_me ));
      foreach ($pageset as $page) {
        $excludeset = get_pages(array( 'child_of' => $page->ID, 'parent' => $page->ID ));
        foreach ($excludeset as $expage) { $exclude_list .= ',' . $expage->ID; }
      }
    }
    
    $thedepth = count($post_ancestors)+1; //prevents improper grandchildren from showing
    
    $children = wp_list_pages(array( 'title_li' => '', 'echo' => 0, 'depth' => $thedepth, 'child_of' => $top_page, 'sort_column' => 'menu_order', 'exclude' => $exclude_list ));

    if ($children) { 
        $subpages = '<ul class="subnav">' . $children . '</ul>';
        return $subpages;
    } else {
        return '';
    }
}