Sunday, 15 September 2013

wp_list_pages, hide parent with no ancestor and show children to chosen post

wp_list_pages, hide parent with no ancestor and show children to chosen post

I'am trying to create a menu using wp_list_pages and I want the end result
to look like this:
parent ( no ancestor ) <-- This one should be hidden
- Children "Some Title"( clicked )
- Children1 to "Some Title"
- Children2 to "Some Title"
- Children "Some other title" ( When clicking on this, children to
"Somte Title" will hide and children to "Some other title" show )
I am using the following code, and I cant figure out how to modify it to
give me the result I want.
<?php
//if the post has a parent
if($post->post_parent){
//collect ancestor pages
$relations = get_post_ancestors($post->ID);
//get child pages
$result = $wpdb->get_results( "SELECT ID FROM wp_posts WHERE
post_parent = $post->ID AND post_type='page'" );
if ($result){
foreach($result as $pageID){
array_push($relations, $pageID->ID);
}
}
//add current post to pages
array_push($relations, $post->ID);
//get comma delimited list of children and parents and self
$relations_string = implode(",",$relations);
//use include to list only the collected pages.
$sidelinks =
wp_list_pages("title_li=&echo=0&include=".$relations_string);
}else{
// display only main level and children
$sidelinks =
wp_list_pages("title_li=&echo=0&depth=1&child_of=".$post->ID);
}
if ($sidelinks) { ?>
<?php echo the_title(); ?>
<ul>
<?php //links in <li> tags
echo $sidelinks;
?>
</ul>
<?php } ?>
The example above gives me a layout looking like this and it shows the
parent with no ancestor and not the sibling to the clicked Children:
parent ( no ancestor )
- Children "Some Title"( clicked )
- Children1 to "Some Title"
- Children2 to "Some Title"
Appreciate any help!

No comments:

Post a Comment