我正在使用基于Genesis的主题,我想构建一个页面/子页面菜单结构。这部分我可以借助比尔·埃里克森(BillErickson)编写的一些代码来完成。
基本上,我正在尝试做的是在包含子页面的页面上的内容上方创建一个菜单。然后,在左侧边栏中,为包含子对象的子页面提供导航。我在这里设置了一些内容:sandbox.digisavvy.com
这是我的代码。
<?php
/**
* Section Menu
* Displays the subpages of the current section
*
* @author Bill Erickson
* @link http://www.billerickson.net/custom-secondary-menu
*/
function be_section_menu() {
// Only run on pages
if( !is_page() )
return;
// If top level page, use current ID; else use highest ancestor
global $post;
$section_id = empty( $post->ancestors ) ? $post->ID : end( $post->ancestors );
// Get all the menu locations
$locations = get_nav_menu_locations();
// Find out which menu is in the \'primary\' location
$menu = wp_get_nav_menu_object( $locations[ \'primary\' ] );
// Grab all menu items in this menu that have a parent of the current section.
// This grabs the subpages, assuming the current section is a top level page
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( \'post_parent\' => $section_id ) );
// If there are menu items, build the menu
if( !empty( $menu_items ) ) {
echo \'<ul class="section-submenu">\';
$first = true;
foreach( $menu_items as $menu_item ) {
$classes = \'page-item\';
// This adds a class to the first item so I can style it differently
if( $first )
$classes .= \' first-menu-item\';
$first = false;
// This marks the current menu item
if( get_the_ID() == $menu_item->object_id )
$classes .= \' current_page_item\';
echo \'<li class="\' . $classes . \'"><a href="\' . $menu_item->url . \'">\' . $menu_item->title . \'</a></li>\';
}
echo \'</ul>\';
}
}
add_action( \'genesis_before_loop\', \'be_section_menu\' );
我想完成的下一部分是创建一个具有子页面/子页面的子页面的菜单系统。这就是我被困的地方。下面的代码被建议作为一种更改,但并不完全起作用。它只是将子对象的子对象添加到导航中。
global $post;
$level = count( $post->ancestors );
// Only build tertiary menu if current page is at least third level
if( 1 > $level )
return;
$section_id = $post->ancestors[$level - 2];