Page – SubPage navigation solution!
-
Big Solution, Minimal Code
I was searching for quite some time to figure out how to develop a navigation system using “Pages” and “Sub-Pages” (page children) that would mimic a traditional static web site.
For example, it would hav a Global Navigation that would display top level “Page” choices horizontally across the header (header.php) and when you clicked on one of the choices you would arrive at that “Page” to find that the sidebar (sidebar.php) would list the child sub pages. When you click on another Global Navigation “Page” choice, a new sub page navigation would display in the sidebar for that page.
The solution I found works in sync with the admin panel, so if you were to develop a site in WP as a Content Management System for a client, they could easily add static sub pages that would update in the sidebars of the corresponding Parent page.
I found the solution within an excellent theme at the Community Theme Viewer. Have a look at the code and check out the authors site TOP NOTCH!!!.
The theme can be found here:
https://themes.wordpress.net/columns/2-columns/296/unsleepable_v16/Test Run the theme, Click on Page 1 and you will see for yourself that the Sub Page navigation updates according to what page you are on!
In Case the theme dissapears at some point here is the code I found to work when I tried it within other themes.
This goes somewhere in the header.php
<ul id=”menu”><?php wp_list_pages(‘sort_column=menu_order&depth=1&title_li=’); ?>
</ul >This goes somewhere in the sidebar.php
<?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'”)) { ?>
<div class=”sb-pagemenu”><h2><?php echo $parent_title; ?> Subpages</h2>
-
<?php wp_list_pages(‘sort_column=menu_order&title_li=&child_of=’. $parent_id); ?>
<?php if ($parent_id != $post->ID) { ?>
“>Back to <?php echo $parent_title; ?>
<?php } ?>
</div>
<?php } } ?>By the way, I am really new to PHP so any questions will likely go over my head (I really just lucked out to find this solution after searching to the wee hours of the morning for a week or so). So, you could ask me if you have difficulties but your best bet is to check out the theme and figure out how it works for yourself. Have Fun!!
IF you are PHP savvy and find bugs in this code or develop this code further please post solution! Also, if you are up for the challenge, you could adapt this code to create what could be the most useful WP plugin ever!!! PLEASE (yes I’m begging) ??
- The topic ‘Page – SubPage navigation solution!’ is closed to new replies.