How to add a subpages menu to WordPress pages

WordPress allows the creation of nested pages. That’s a great feature when you need to split a rather long content into several parts and give them some structure. However, the problem is you’ll have to manually add links to the additional pages in order to make it easy for the user to navigate through all the content. Wouldn’t it be great if you could automatically add a menu, right inside the content of your pages, with links to the subpages or parent of current page? I think it would, and I’ll show you how…

The snippet above will check if the current page is a subpage and add a link to the parent page (the one directly above and not the one at the very top of the hierarchy). Then, if the current page has subpages will add links to each one of them.

The purpose of the snippet is to show how to get the links to the parent and children pages. The function may, and probably should, be extended to properly display that information in your themes.