thanks davidchait,
i have used your function in the following code.
<?php
function is_page_or_subpage($id)
{ /* this can be used instead of is_page FOR PAGE ID TESTS ONLY */
global $wp_query;
if (! $wp_query->is_page) {
return false;
}
if (empty($id)) {
return true;
}
$page_obj = $wp_query->get_queried_object();
if ($id == $page_obj->ID) {
return true;
}
$pages = get_pages();
foreach ($pages as $page) {
if ($page->ID == $page_obj->ID /* found the page we're on */
&& $page->post_parent == $id) /* parent matched the passed ID! */
return true;
}
return false;
}
?>
<!-- Pages Fruit -->
<?php if(is_page_or_subpage(5)): ?>
<a href='<?php echo get_settings('home');?>/fruit'>Fruit</a><br>
<ul>
<?php wp_list_pages('title_li=&exclude=2,3,4,6,7,8,9,10,11,12,13&sort_column=post_name&child_of=5' ); ?>
</ul>
<?php else : ?>
<a href='<?php echo get_settings('home');?>/fruit'>Fruit</a><br>
<ul>
</ul>
<?php endif; ?>
<!-- Pages Animals -->
<?php if(is_page_or_subpage(7)): ?>
<a href='<?php echo get_settings('home');?>/animals'>Animals</a><br>
<ul>
<?php wp_list_pages('title_li=&exclude=2,3,4,5,6,8,9,10,11,12,13&sort_column=post_name&child_of=7' ); ?>
</ul>
<ul>
</ul>
<?php else : ?>
<a href='<?php echo get_settings('home');?>/animals'>Animals</a><br>
<ul>
</ul>
<?php endif; ?>
How to:
Just paste the above code into your sidebar.php
In the above example, page “Fruit” has the ID 5.
Add the ID in:
1) is_page_or_subpage(5)
2) child_of=5
Add exclude IDs:
Here one enters the
exclude=2,3,4,6,7,8,9,10,11,12,13
Notice in this example that the 5 is not in the “exclude” list.
Add the Links:
The links are hardcoded URLs. It would be naturally
more elegant if these URLs could be created from the ID
but my knowledge of the WordPress system isn’t that great.
repeat for Pages Animals, etc.
this works very well for me. of course the top level (parent pages) need to be
hard coded, as in the above example however the children pages will be
dynamically displayed. this would work fine for a simple CMS site.
I guess this only works for a two level hierachy though.
thought this might help someone out there.
steven
ps. i have added this post into a text file at:
https://www.fluxmedia.de/images/WP_collapsable.txt
because this post seems to clip the length on some of the code lines.