display specific top level and child pages in word

2019-08-30 05:22发布

问题:

Have been searching for an answer to this for a while, have seen lots of answers on various menu solutions but none of them quite deliver what I am loking for.

I am trying to display the child pages and the parent page on a menu within wordpress. So if my site lookslike this:

page 1

page 1a

page 1b

page 1c

page 2

page 3

page 3a

page 3b

page 4

page 5

When on Page 1, I want to show Page 1 and all the child pages below on a menu, when on a child page I want to show the child pages and the parent page on the menu, but not the rest of the pages on the top level.

My current code gets me halfway there as it display all the child pages on any page of the specific branch as it has a specific post id within it, but I don't know what to add to create the parent id. Current code I am using is below:

<ul>
<?php
  $children = wp_list_pages('title_li=&child_of='.$post->ID.'820');
  if ($children) { ?>
  <ul>
  <?php echo $children; ?>
  </ul>
  <?php } ?>
</ul>

回答1:

Much as I don't want to answer 'use a plugin' on SO, that's exactly what I'm going to do - or at least, point you in the direction of a plugin which you can then take the code from and convert and add to your theme if you want. It's only a single and quite small php file.

An oldie but a goodie and I still use it now (even though it's not been updated in years it still works fine on the latest 3.4.1;

Wordpress Subpages Widget

It is set up as a widget but obviously you wouldn't need to use it as such. It is a handy bit of code, it will display links to child pages but also links to sibling pages (which I understand as the second part of your question), and show the link to the parent page too.