Skip to content

Instantly share code, notes, and snippets.

@levymetal
Last active November 1, 2022 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save levymetal/5064699 to your computer and use it in GitHub Desktop.
Save levymetal/5064699 to your computer and use it in GitHub Desktop.
Custom Wordpress function which displays a list of child pages from a common parent, which can be called from either the parent page (displays children) or any of the child pages (displays siblings).
<?php
function my_custom_submenu() {
global $post;
$menu_items = wp_get_nav_menu_items('Menu');
$current_menu_id = null;
// get current top level menu item id
foreach ( $menu_items as $item ) {
if ( $item->object_id == $post->ID ) {
// if it's a top level page, set the current id as this page. if it's a subpage, set the current id as the parent
$current_menu_id = ( $item->menu_item_parent ) ? $item->menu_item_parent : $item->ID;
break;
}
}
// uncomment this line if you don't want to display an empty ul
// if ( $current_menu_id == null ) return;
// display the submenu
echo "<ul id='supplementary_menu'>";
foreach ( $menu_items as $item ) {
if ( $item->menu_item_parent == $current_menu_id ) {
$class = ( $item->object_id == $post->ID ) ? "class='current_page_item'" : "";
echo "<li {$class}><a href='{$item->url}'>{$item->title}</a></li>";
}
}
echo "</ul>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment