One of the things that is starting to happen at OLT is that we are creating an increasing number of WordPress based websites. Using WordPress as a content management system is not a new idea at all, there are a ton of examples out there of WordPress blogs out there that have been turned into sites. There is however, a dearth of information out there on how to do it (there are some out there… including some in amazing detail from Alan Levine).
One of the things that I couldn’t find was a stable way to create a second level navigation that stays constant for every top level section. The problem with most of the solutions on the forums and sites around is that as soon as you drill down to the third level of navigation the second level disappears. As you can see at aboriginal.ubc.ca I was able to come up with a way to keep the navigation constant. Here is the loop that I had to create:
<?php
$secondAncestor = count($post->ancestors) -1; //figure out what level of navigation we are on, subtract one because we don't want to consider the top-level
if($post->post_parent!=0) //if the page is not a top-level category
{
echo '<h2 class="widgettitle">In this section:</h2><li class="sidebarlist">';
//the following lists children of second level ancestor of the current page.
wp_list_pages("title_li=&child_of=".$post->ancestors[$secondAncestor]."& sort_column=menu_order&echo=1");
echo '</li>';
}
else //if the page is a top-level category
{
//listing only the child pages of the current section
$children= wp_list_pages("title_li=&child_of=".$post->ID."& sort_column=menu_order&echo=0");
if($children) //this will stop it from displaying a section heading if there are no elements in the section (for example on the home page)
{
echo '<h2 class="widgettitle">In this section:</h2><li>';
echo $children;
echo '</li>';
}
}
?>
This is the first time I’ve blogged code, I’m not even sure if it is readable… but here’s hoping. Basically I figure out what level of navigation the user is on and then list the pages of the current page’s ancestor… that many levels up (subtracting one for the top level navigation.
I am currently doing a lot of work on using WordPress as a content management system including coming up with plugins and modifications for using WordPress MU as a multi-site manager used purely for websites and not for blogs. Will blog it all once everything is stable and working.
I don’t think we here at UMW have to tell you how much we anticipate your updates on this. Go Andre, Go!
Thanks to cats (or is it dogs?) like you and Alan a prima donna like me never has to get his feet muddy in the code. This is amazing Andre, and you have no idea how excited I am to find out what you have been up to. For making WPMu an effective and functional CMS could have some major impact here at UMW, and for the faculty and student more broadly. Thanks for sharing 🙂
We just started using WP as a CMS for our TTIX.org conference site last year, and have major plans for expansion this year. WP is a perfect marriage of easy expandability of features and user administration.
I’ve been tearing my hair out desperately trying to do (on a deadline) exactly what you’ve accomplished in the example: first level nav recognition that sticks when you get to the third level page.
However, when I tried to use the code you posted in my sidebar.php, the page doesn’t render at all. I tried it exactly as-is, then removed references to styles to make sure it wasn’t getting confused, but still nothing. There seemed to be a “echo ‘’;” missing from that last chunk of code, but other than that, I don’t know what’s wrong with it.
Am i putting this code in the right place? I’m using the Cleaker theme, if that matters at all…
I hate to floor your comments with this problem, I’d email you if I could…
OK, I’ve got the pages rendering again (some small copy/paste find/replace bits I didn’t catch the second or third time around), but I’m still having a problem.
I have the same setup as the example: main page nav at the top, 2nd and 3rd level nav in the sidebar. Everything looks as it should on a main page (save for some pesky bullets I need to get rid of), but if I go to a second or third level page, I get *everything* in the navigation tree, all pages, child pages, and grandchild pages.
This is what the code looks like:
post;
$secondAncestor = count($thisPage->ancestors)-1; //figure out what level of navigation we are on, subtract one because we don't want to consider the top-level
if($thisPage->post_parent!=0) //if the page is not a top-level category
{
//the following lists children of second level ancestor of the current page
wp_list_pages("title_li=&child_of=".$thisPage->ancestors[$secondAncestor]."&sort_column=menu_order&echo=1");
}
else //if the page is a top-level category
{
//listing only the child pages of the current section
$children= wp_list_pages("title_li=&child_of=".$thisPage->ID."&sort_column=menu_order&echo=0");
if($children) //this will stop it from displaying a section heading if there are no elements in the section (for example on the home page)
{
echo $children;
}
}
?>
Any idea what the weirdness might be?
Hey Andrea,
I don’t see it in the snippet that you posted, did you include the first line “$thisPage = $wp_query->post;”?
Code updated and simplified a bit based on Andrea’s feedback.
We just released a plugin that makes adding this kind of navigation very easy:
http://www.cmurrayconsulting.com/software/wordpress-simple-section-navigation/
Would love feedback!
this helped like you wouldn’t belieeeeve
Glad that it helped.
Thank you so much. This is the most helpful wordpress CMS navigation solution I have found.
Wow, wish this was built into wordpress, excellent solution, thanks!