Created
May 19, 2010 23:08
-
-
Save whalesalad/406973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| I use this function for every single WP project. | |
| It's essentially a drop-in way for a surefire, reliable, primary navbar. | |
| Pages with a -1 index aren't shown. | |
| Sub-level pages aren't shown either, but if you're on a sub-page the parent is given an active class. | |
| First and last links are given classes (useful for if you're adding separators to your menu) | |
| It's legit. | |
| P.S. I ripped it out of my Template class, hence the public static bit. | |
| */ | |
| public static function primary_nav() { | |
| // The site-wide navigation | |
| $p = get_pages(array('parent' => 0, 'sort_column' => 'menu_order', 'hierarchical' => 0)); | |
| $pages = array(); | |
| global $wp_query; | |
| if (is_page() || is_attachment() || $wp_query->is_posts_page) | |
| $current_page = $wp_query->get_queried_object_id(); | |
| foreach ($p as $key => $page) { | |
| $css_class = array('page', 'page_item', 'page-item-'.$page->ID); | |
| // Handle the first link in the list | |
| if ($key == 0) | |
| $css_class[] = 'first'; | |
| // Handle the last link in the list | |
| if ($key == (count($p) - 1)) | |
| $css_class[] = 'last'; | |
| // If we're on the current page, simply make it active | |
| if ($page->ID == $current_page) | |
| $css_class[] = 'active'; | |
| if (is_single() && $page->post_name == 'blog') { | |
| $css_class[] = 'active'; | |
| } | |
| // If we're on a sub page, make the parent page active. | |
| if (!empty($current_page)) { | |
| $_current_page = get_page($current_page); | |
| if (isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors)) | |
| $css_class[] = 'active'; | |
| elseif ($_current_page && $page->ID == $_current_page->post_parent) | |
| $css_class[] = 'active'; | |
| } elseif ($page->ID == get_option('page_for_posts')) { | |
| $css_class[] = 'active'; | |
| } | |
| $subnav = unserialize(base64_decode(get_post_meta($page->ID, '_subnav_description', true))); | |
| // Give pages a -1 menu order for them to not appear here | |
| if ($page->menu_order >= 0) { | |
| $pages[$key] = sprintf('<li class="%s"><a href="%s">%s <span>%s</span></a></li>', | |
| implode(' ', $css_class), get_page_link($page->ID), $page->post_title, stripslashes($subnav)); | |
| } | |
| } | |
| return implode("\n", $pages); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment