Skip to content

Instantly share code, notes, and snippets.

@FriendlyWP
Created October 12, 2016 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FriendlyWP/a1aebbba4dfb54659652a2abd404f6cb to your computer and use it in GitHub Desktop.
Save FriendlyWP/a1aebbba4dfb54659652a2abd404f6cb to your computer and use it in GitHub Desktop.
Sidebar menu shortcode
add_shortcode('sidebar-menu','fwp_simplesidenav');
function fwp_simplesidenav( $atts, $content = null) {
global $post;
$current_page = $post->ID;
extract( shortcode_atts( array(
'exclude' => '',
'exclude_children_of' => '',
'exclude_tree' => '',
'children_of' => $current_page,
'type' => 'list',
'children' => 'true',
'siblings' => 'false',
'show_root_only' => 'false',
'siblings_of' => $current_page,
'depth' => '',
'showtree' => 'false',
'group' => 'false',
'inline' => 'false',
'dropdown_default' => 'Select',
), $atts ) );
// establish variables
$output = '';
//$include = '';
$thedepth = 0;
$excluded_children = '';
if ( $siblings == 'true' ) {
$children_of = $post->post_parent;
}
if ( $group !== 'false' ) {
$group = 'group';
} else {
$group = 'flat';
}
$exclude_list = $exclude;
//convert list of excluded pages to array
$excluded = explode(',', $exclude_list);
// if on excluded page, do not show list at all
// if ( in_array($post->ID,$excluded) ) return false;
//get the current page's ancestors either from existing value or by executing function
$post_ancestors = ( isset($post->ancestors) ) ? $post->ancestors : get_post_ancestors($post);
//get the top page id
$top_page = $post_ancestors ? end($post_ancestors) : $post->ID;
if ( $showtree === 'true' ) {
$children_of = $top_page;
}
// FOR DEFAULT LISTS, EXCLUDE PAGES NOT IN DIRECT HIERARCHY
$ancestors_me = implode( ',', $post_ancestors ) . ',' . $post->ID;
foreach ($post_ancestors as $anc_id) {
//if ancestor excluded, and hide on excluded, leave
if ( in_array($anc_id,$excluded) && $instance['hide_on_excluded'] ) return false;
$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; }
}
}
// IF SHORTCODE LISTS ANY PAGES WITH EXCLUDED CHILDREN
if ($exclude_children_of !== '') {
// convert exclude_children_of from string into array
$excluded_parent_array = explode(',', $exclude_children_of);
foreach ($excluded_parent_array as $excluded_parent) {
// using helper function my_get_page_children get IDs of child pages to exclude for this parent ID
$excluded_parent = $this->my_get_page_children($excluded_parent);
foreach ($excluded_parent as $excludedpage) {
// concatenate all the child ids into one list
$excluded_children .= ', ' . $excludedpage;
}
}
//echo $excluded_children;
// print_r($excluded_children);
$excluded_children_array = explode(',', $excluded_children);
$exclude_list_array = explode(',', $exclude_list);
$excluded_list = array_merge($exclude_list_array, $excluded_children_array);
$excluded_list = array_unique($excluded_list);
$csv_excluded_list = implode(",", $excluded_list);
} else {
$csv_excluded_list = $exclude_list;
}
$parent_link = get_permalink($children_of);
$parent_title = get_the_title($children_of);
// the unordered list
$the_list = wp_list_pages(array( 'title_li' => '', 'echo' => 0, 'depth' => $thedepth, 'child_of' => $children_of, 'sort_column' => 'menu_order', 'sort_order' => 'ASC', 'exclude' => $csv_excluded_list, 'exclude_tree' => $exclude_tree, ));
if ($type === 'list' && $the_list) {
$output = '<ul class="parent-sidebar-menu"><li><a href="' . $parent_link . '">' . $parent_title . '</a><ul class="child-sidebar-menu">' . $the_list . '</ul></li></ul>';
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment