Welcome back! Have you subscribed to my RSS feed? If not, please consider doing so. Thanks for again for visiting!
When you dice the theme, we will find that aside from the usual page tag which looks like this:
You will notice we have added the conditional tag to call child pages within the same “div”, which looks like this:
<?php global $notfound; ?>
<?php /* Creates a menu for pages beneath the level of the current page */
if (is_page() and ($notfound != '1')) {
$current_page = $post->ID;
while($current_page) {
$page_query = $wpdb->get_row("SELECT ID, post_title, post_status,
post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
$current_page = $page_query->post_parent;
}
$parent_id = $page_query->ID;
$parent_title = $page_query->post_title;
if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?> <li>Subpage:</li> <?php wp_list_pages('sort_column=menu_order&title_li=&child_of='. $parent_id); ?>
<?php } } ?>
If you look closely at what the conditional code is required to do, you will find that it is designed to display sub-pages when parent pages with sub-pages are visited. No sub-pages are displayed whe you are on pages without sub-pages or on other post or category page templates.
All you need to do is add the above code within the same “div” as the main navigation menu (either in the header or sidebar) and that is pretty much it. So there you have it, the simple conditional navigation menu. Next time, we will dig into dropdown menus using CSS and JavaScript.
Use SimpleCode while including codes.
The links from all comments have the no-follow tag to prevent spammers from gaming their way up into SERP. So please keep the comments clean, try not to use keywords as "Names" or post unnecessary links in the comments. Thank you for cooperating.
Much appreciated! Thanks guys!
Thanks very for this useful tutorial. It is inspiring me to create my own wp theme