我需要一种为wp\\u nav\\u菜单功能创建自定义HTML模板的方法。我听说过定制步行者课程,但这些课程似乎没有足够的帮助来实现我想要做的事情;至少据我所知,因为缺少有关walker函数的文档。我需要做的是能够向所有顶级菜单项添加一个“可悬停”类。我只需要两层菜单;顶层,然后是子菜单项。我需要向所有具有子菜单的菜单项锚元素添加一个“顶级”类。我需要所有子菜单列表来设置“sub nav”类。我需要所有最后的子菜单列表项(li)才能有一个类“last”。
下面是我现在拥有的代码,它按照我需要的方式使用get\\u pages函数生成菜单:
<?php
$pages = get_pages(array(
\'parent\' => 0,
\'sort_order\' => \'ASC\',
\'sort_column\' => \'menu_order\'
));
$num_pages = count($pages);
$p = 0;
$exclude = \'"pastor.php","service.php","gallery.php","audio.php","video.php"\';
$exclude_list = $wpdb->get_results("SELECT GROUP_CONCAT(t1.ID) AS IDS FROM " . $wpdb->posts . " AS t1 INNER JOIN " . $wpdb->postmeta . " AS t2 ON (t1.ID = t2.post_id) WHERE t1.post_type = \'page\' AND (t1.post_status = \'publish\' OR t1.post_status = \'private\') AND t2.meta_key = \'_wp_page_template\' AND t2.meta_value IN (" . $exclude . ") ORDER BY t1.post_date DESC");
foreach($pages as &$page) :
$children = get_pages(array(
\'sort_order\' => \'ASC\',
\'sort_column\' => \'menu_order\',
\'hierarchical\' => 0,
\'childof\' => $page->ID,
\'parent\' => $page->ID,
\'exclude\' => $exclude_list[0]->IDS
));
$num_children = count($children);
$has_children = $num_children > 0;
?>
<li class="nav-item<?php echo ($has_children ? \' hoverable\' : \'\') . ($num_pages == ++$p ? \' last\' : \'\') . ($page->post_name === $root_parent->post_name ? \' active\' : \'\')?>">
<a href="<?php echo get_page_link($page->ID)?>" class="top-level"><?php echo $page->post_title?></a>
<?php if($has_children) : ?>
<ul class="sub-nav">
<?php
$c = 0;
foreach($children as &$child) : ?>
<li class="nav-item<?php echo ($num_children == ++$c ? \' last\' : \'\')?>">
<a href="<?php echo get_page_link($child->ID)?>"><?php echo $child->post_title?></a>
</li>
<?php endforeach;?>
</ul>
<?php endif;?>
</li>
<?php
endforeach;
?>
有没有一种方法可以在有序的多维数组中提取菜单项,这样我就可以迭代它们并手动生成上面的模板,而不是所有这些wp\\u nav\\u menu和walker none sense?
SO网友:ifdion
是的,有。使用wp_get_nav_menu_items()
相反
<?php $items = wp_get_nav_menu_items( $menu, $args ); ?>
<?php $args = array(
\'order\' => \'ASC\',
\'orderby\' => \'menu_order\',
\'post_type\' => \'nav_menu_item\',
\'post_status\' => \'publish\',
\'output\' => ARRAY_A,
\'output_key\' => \'menu_order\',
\'nopaging\' => true,
\'update_post_term_cache\' => false ); ?>
这是上的页面
WordPress Codex