• Hello,

    I’m using wordpress 2.7 and I’m trying to find a way to list pages and only current page’s subpages in the sidebar.
    Till now, I had little success.

    Anyone can help me?

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Any luck with this?

    i too have a requirement to only list the subpages of page when that page is the current page selected.

    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>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sidebar – List Pages and Only Current Page’s Subpages’ is closed to new replies.