dekraan
Member
Posted 2 years ago #
At the moment I have this code, to add a subpage menu in the sidebar on a parentpage:
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
I am looking for a way to add the parent-page to this menu, as a link. That way, when you click a subpage, you can still get back to the parent-page through the sidebar-menu!
Use
<?php
if($post->post_parent){
$parent=get_post($post->post_parent);
$children = '<li><a href="'.get_permalink($post->post_parent).'">Parent:'.$parent->post_title.'</a></li>';
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
}else{
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
I haven't tested this but I'm pretty confident it should work. If it doesn't work as expected get back to me.
dekraan
Member
Posted 2 years ago #
Hello tomontoast!
It almost does the trick. When you look at http://www.customizen.nl and then click on 'over ons' you see in the sidebar 'hello'. That is a subpage. When you click it, you see the page itself, and in the menu 'parent:over ons' has appeared!
So I see two problems. One: it isn't in the menu straight away, but only on the subpage-menu. Two: there is 'parent:' in front of it!
Do you know what is wrong?
Firstly, you can put it on the first menu if you want but I see that as a bit pointless seeing as you are already on that page. Secondly I wrote the parent bit in to show that it is a parent page but you don't have to have that either.
Heres the updated code:
<ul><?php
if($post->post_parent){
$parent=get_post($post->post_parent);
$children = '<li><a href="'.get_permalink($post->post_parent).'">'.$parent->post_title.'</a></li>';
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
}else{
$children = '<li><a href="'.get_permalink($post).'">'.$post->post_title.'</a></li>';
$children .= wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}
echo $children; ?>
</ul>
dekraan
Member
Posted 2 years ago #
This works! Thank you very much. I do need the parent to show up, so thank you very much for editing the code!