I'm trying to deal with Wordpress 3.0. It's rather cool thing but I can't get on with one problem. For example, I have such menu tree. Menu tree is constructed from pages.
Home
news
video
audio
Blog
About author
Favourite colors
red
blue
green
My car
wheels
tires
The Idea is: main menu consists of root elements: home, blog, my car On the left side I would like to display children elements of current active root element.
For exammple if person is on the "home" page, on the left part he should see:
news
video
audio
If user is on the "Blog" page, he should see:
About author
Favourite colors
red
blue
green
I can't find an API to do that. Can you sugest me please where can I find it?
UPD: @Jason McCreary I've seen I've seen wp_list_pages() and tried it. I din't get how can I use it: Please, see my template for a page:
<?php
/*
Template Name: page_news
* @package WordPress
* @subpackage Twenty_Ten
* @since Twenty Ten 1.0
*/
get_header(); ?>
<h1>page_news</h1>
<h1>Children menu:</h1>
<?php wp_list_pages('echo=0&child_of=8&title_li='); ?>
<div id="container">
<div id="content" role="main">
<?php
/** Get category id by name*/
//$catId = get_category_by_slug('news')->term_id;
query_posts('category_name=news');
get_template_part( 'loop', 'page' );
?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
See this line of code:
<?php wp_list_pages('echo=0&child_of=8&title_li='); ?>
I do have the page with id=8 (I see it in URL). Page with id=8 has several children. I want to print them, but they are not printed. The output of the function wp_list_pages() is nothing. I don't know why... :(
You can write a filter_hook to accomplish this.
My method: create an additional start_in
argument for wp_nav_menu
using my custom hook:
# in functions.php add hook & hook function
add_filter("wp_nav_menu_objects",'my_wp_nav_menu_objects_start_in',10,2);
# filter_hook function to react on start_in argument
function my_wp_nav_menu_objects_start_in( $sorted_menu_items, $args ) {
if(isset($args->start_in)) {
$menu_item_parents = array();
foreach( $sorted_menu_items as $key => $item ) {
// init menu_item_parents
if( $item->object_id == (int)$args->start_in ) $menu_item_parents[] = $item->ID;
if( in_array($item->menu_item_parent, $menu_item_parents) ) {
// part of sub-tree: keep!
$menu_item_parents[] = $item->ID;
} else {
// not part of sub-tree: away with it!
unset($sorted_menu_items[$key]);
}
}
return $sorted_menu_items;
} else {
return $sorted_menu_items;
}
}
Next, in your template you just call wp_nav_menu
with the additional start_in
argument containing the ID of the page you want the children off:
wp_nav_menu( array(
'theme_location' => '<name of your menu>',
'start_in' => $ID_of_page,
'container' => false,
'items_wrap' => '%3$s'
) );
I wrote this to print sub-navs of the pages you may be on. If you want to print out the sub-navigation for each of the pages, get the page parent instead of getting the ID. There would be more involved than that, but it's a start.
$menu = wp_get_nav_menu_items( 'Primary Menu' );
$post_ID = get_the_ID();
echo "<ul id='sub-nav'>";
foreach ($menu as $item) {
if ($post_ID == $item->object_id) { $menu_parent = $item->ID; }
if (isset($menu_parent) && $item->menu_item_parent == $menu_parent) {
echo "<li><a href='" . $item->url . "'>". $item->title . "</a></li>";
}
}
echo "</ul>";`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With