Page and Sub-page Menus In Wordpress
Written by Chris on September 17, 2007 – 6:52 pm -Recently I wanted to change the theme I use on my personal blog to the following: http://www.wpdesigner.com/2007/01/22/digg-3-columns-wordpress-theme/ as the current one I was using was pretty awful (to say the least) and this was a lot better
I liked this theme but unfortunately it lacked one thing (as do a lot of other themes I've used) - an auto generating set of page menu tabs at the top. The default theme just had a link to the home and about pages, so I added this in and it now works exactly the way I wanted it to. I've pasted the code below should you wish to do something similar.
This mod basically creates a list of all the parent pages along the top (which are rendered as tabs due to the css), and then when a link of a parent page is clicked, it displays the home tab, the tab for the parent page and then the list of tabs for the sub pages of that parent page.
On my site so far I only have one level of sub pages below the parent, but I believe this should work for any hierarchy of sub-sub pages - if you use this code and it does please post a comment to let me know.
Also this is a bit rough - the next time I use it I will probably clean it up a bit, but I know it works for now. If you'd like to provide me with a cleaner version before I have chance to work on it please feel free to drop me a line, and if it works I'll also post it here.
-
-
<div id="header">
-
-
<div id="menu">
-
<ul>
-
<li><a href="<?php bloginfo('url'); ?>/" title="<?php bloginfo('name'); ?>"><?php _e('Home'); ?></a></li>
-
-
-
<?php
-
-
// From http://codex.wordpress.org/Template_Tags/wp_list_pages
-
-
'show_date' => '',
-
'date_format' => get_option('date_format'),
-
'child_of' => 0,
-
'exclude' => '2, 53',
-
'title_li' => '',
-
'echo' => 1,
-
'authors' => '',
-
'sort_column' => 'menu_order, post_title');
-
-
if($post->post_parent)
-
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
-
else
-
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
-
-
if ($children)
-
{
-
{
-
wp_list_pages('title_li=&include='.$post->post_parent);
-
}
-
else
-
{
-
?>
-
<li><a href="<?php echo get_permalink($post->ID); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
-
<?php
-
-
-
-
}
-
echo $children;
-
}
-
else
-
{
-
?>
-
<li><a href="<?php echo get_permalink(2); ?>" title="<?php _e('About'); ?>"><?php _e('About'); ?></a></li>
-
<?php
-
wp_list_pages($defaults);
-
}
-
?>
-
-
</ul>
-
</div>
-
Popularity: 18% [?]
Sphere: Related ContentRelated posts on coderchris.com:
- Make Money With Wordpress And Matched.co.uk
Everybody out there at the moment is trying to make money from their blog... - Advanced Syntax-Hilighting Online Code Editors - A Wordpress IDE?
When I write plugins for wordpress I like to do it on a test... - Dodgy Wordpress Plugins and Themes
Recently I have been doing a LOT of research into various wordpress themes and... - Wordpress 2.5 Released!
Version 2.5 of wordpress has been released and we have a nice new admin interface... - Wordpress 2.3 has been released
Wordpress 2.3 has been released and I can already see the improvements as I'm...
Tags: menu, page, sub, sub-page, subpage, theme, Wordpress
Posted in PHP, Wordpress | Trackback |















Feed 

September 2nd, 2008 at 10:57 pm
wow
its very reasonable article.
Good post.
realy gj
thank you
December 11th, 2008 at 2:15 pm
Thank you for this information.