I found WordPress plugin: Flexi Pages Widget | Srini’s WordPress Laboratory and extracted the code for resolving our issue.
I’m very new to php programming so use at your own risk, here’s the code.
<?php
function pnp_get_subpages()
{
global $wpdb;
$sql = "SELECT ID, post_title, post_parent FROM ".$wpdb->posts;
$sql .= " WHERE
post_type = 'page'
AND post_status = 'publish'
AND post_parent <> 0 ";
if($sub_pages = $wpdb->get_results($sql, ARRAY_A))
return $sub_pages;
else return array();
}
function pnp_current_page_hierarchy()
{
if( !is_page() )
return array();
global $post;
$current_page = $post;
// get parents, grandparents of the current page
$hierarchy[] = $current_page->ID;
while($current_page->post_parent) {
$current_page = &get_post($current_page->post_parent);
$hierarchy[] = $current_page->ID;
}
return $hierarchy;
}
function pnp_get_exclude_page_ids()
{
$sub_pages = pnp_get_subpages();
$exclude = array();
$hierarchy = pnp_current_page_hierarchy();
foreach ($sub_pages as $sub_page) {
// if the parent of any of the subpage is not in our hierarchy,
// add it to the exclusion list
if ( !in_array ($sub_page['post_parent'], $hierarchy) )
$exclude[] = $sub_page['ID'];
}
return implode(',', $exclude);
}
?>
<div>start menu</div>
<ul id="primary">
<li<?php if (is_home()) echo ' class="current_page_item"'; ?>><a href="<?php echo get_settings('home'); ?>/">HOME</a></li>
<?php
$args = array (
'depth' => 0,
'show_date' => null,
'date_format' => get_option('date_format'),
'child_of' => 0,
'exclude' => pnp_get_exclude_page_ids(),
'title_li' => null,
'echo' => 1,
'authors' => null,
'sort_column' => 'menu_order, post_title',
'link_before' => null,
'link_after' => null,
'exclude_tree' => null
);
wp_list_pages($args);
?>
</ul>
<div>end menu</div>