Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Create sidebar menu WordPress

  • SOLVED

Hi,
I've a website where I mainly use pages, and there are 4 main pages, the rest are childs of those.
I want to display list of the childs in the sidebar depending on the page you are in.
I achieved to list them correctly by checking the ID using the code, but it only works in the parent page, I need it in the child page as well:

<ul>
<?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); if ($children) { ?>
<li class="widget">
<div class="title"><h5>Menu</h5></div>
<div class="content">
<ul><?php echo $children; ?></ul>
</div>
</li>
<?php } ?>
</ul>

on the other side I also need to have listed some of the other pages, for example:
if you are on page with ID 1 or it's childs, it will list on top (of the sidebar) 10 of the childs of ID 1, followed by 5 of ID 2, 5 of ID 3, and 5 of ID 4; then if you are on page with ID 2 or it's childs, it will list on top 10 of the childs of ID 2, followed by 5 of ID 1, 5 of ID 3, and 5 of ID 4, and so on for the other IDs.

Is this possible, can someone help me here

thank you

Answers (3)

2012-07-28

Martin Pham answers:

You can find answers in
[[LINK href="http://wpquestions.com/question/showLoggedIn/id/4265"]]http://wpquestions.com/question/showLoggedIn/id/4265[[/LINK]]


Martin Pham comments:

Try this

// Put on functions.php
function page_menu_list() {
global $post;
if ($post->post_parent) {
$parents = get_post_ancestors($post->ID);
$parent = $parents[count($parents)-1];
} else {
$parent = $post->ID;
}
// Dislay curent page, child of curent page or parent
$page_nav = wp_list_pages( 'title_li=&child_of='.$parent.'&echo=0' );
// Display all top page (1-2-3-4)
$all_top_page = wp_list_pages( 'title_li=&exclude_tree='.$parent.'&depth=1&echo=0' );
printf('<div class="widget"><ul>%s</ul><ul>%s</ul></div>', $page_nav, $all_top_page);
}


Using


<?php page_menu_list(); ?>


Martin Pham comments:

Update function


function page_menu_list() {
global $post;
if ($post->post_parent) {
$parents = get_post_ancestors($post->ID);
$parent = $parents[count($parents)-1];
} else {
$parent = $post->ID;
}
// Dislay curent page
$page_nav = wp_list_pages( 'title_li=&child_of='.$parent.'&echo=0' );
// Display all top page (1-2-3-4)
$all_top_page = wp_list_pages( 'title_li=&exclude_tree='.$parent.'&depth=1&echo=0' );
printf('<div class="widget menu-page-widget">
<h3 class="curent-parent-page"><a href="%s">%s</a></h3>
<ul>%s</ul>
<h3 class="other-top-page">Other Page</h3>
<ul>%s</ul>
</div>',
get_page_link($parent),
get_the_title($parent),
$page_nav,
$all_top_page
);
}


Pls, View attachfile for example.