For those beginner developers out there working in WordPress, here’s a little trick that you may have not run into yet. If you need to build out a website in WordPress that will load the navigation dynamically, meaning that any pages that are added in the backend of WordPress are automatically placed in the navigation on the front end of the website, there is a nice piece of code that will do that for you. You have the option to edit that code to render out the displayed navigation in almost any way that you’d like, which is a great plus when it comes to having control over a website.
The trick comes into play if you decide that you only want WordPress to output the parent navigation, ignoring any child pages that may be in WordPress. So here is the code that calls in the navigation dynamically as is:
<?php wp_list_pages(); ?>
So all we need to do if we decide that we only want to show the parent navigation is add a small piece of code in between the brackets.
<?php wp_list_pages('depth=1'); ?>
What we’re telling WordPress to do here is to create a list of the pages that we have in the backend of WordPress and only show the top level navigation. If we were to change that 1 to a 2, WordPress would output the top level and second level navigation, ignoring anything beyond that! For more information on the wp_list_pages tag check out the following link: