Better WordPress Subpage Navigation
Wordpress was originally designed as a blogging platform, and has evolved into a full-blown content management system. For all of its greatness, there are a few features that just seem to be missing.
On nearly every site I build, I use a sub-page navigation widget in the page sidebar. This could be accomplished with some of the built in widgets such as the ‘Pages’ or ‘Custom Menu’ widget, but each of these has shortfalls. The ‘Pages’ widget simply displays a list of all of the pages in the system, which is less than ideal if there are more than a handful. The ‘Custom Menu’ widget could work in some implementations, but it requires time consuming maintenance each time a page is added to the site, and a custom menu would need to be created for each page and a unique sidebar for each page as well.
Given these limitations, and my failure to find a sub-page navigation widget which does exactly what I want, I have created solution which creates a nice looking menu which is generated automatically.
[code language=”php”]
/* NAVIGATION WIDGET SHORTCODE */
function nav_shortcode_func() {
global $post;
$a .= ‘<ul class=”menu”>’;
if($post->post_parent != 0) {
$a .= ‘<li class=”menu-item”><a href=”‘ .
get_page_link($post->post_parent) . ‘”>’ .
get_the_title($post->post_parent) . ‘</a></li>’;
}
$a .= ‘<li class=”menu-item”><a href=”‘ .
get_page_link($post->ID) . ‘”>’ .
get_the_title($post->ID) . ‘</a></li>’;
$pages = get_pages(‘child_of=’.$post->ID.’&sort_column=post_title’);
foreach($pages as $page) {
$a .= ‘<li class=”menu-item”><a href=”‘ .
get_page_link($page->ID) . ‘”>’ .
$page->post_title . ‘</a></li>’;
}
$a .= ‘</ul>’;
return $a;
}
add_shortcode( ‘nav-widget’, ‘nav_shortcode_func’ );
[/code]
Adding this code to your theme’s functions.php file will create a shortcode which can then be used in a standard ‘Text’ widget by typing [nav-widget] and saving the widget contents. This widget will display a menu showing the current page’s parent (if it has one), followed by the current page, followed by any direct children of the page. This simple code works for 95% of implementations in my experience. If you’d like to see me turn this into a plugin, leave a comment and I’ll see what I can do.
If your website needs improvement, get in touch with A. Thiel Marketing today to find out how we can help.
Categories
Recent posts
- WordPress / PHP Web Developer/Programmer Chatham Ontario Job Vacancy July 28, 2014
- Don’t be a victim of blackmail June 30, 2014
- Buying Links For Your Website To Help SEO April 22, 2014
- Domain Registry of Canada Requests – SCAM July 30, 2013
- Customizing the Admin Panel CSS (The Smart Way) July 03, 2013