基于您的评论,mashable。com菜单是您想要的,我假设您希望HTML的结构与之类似(也就是说ul.menu > li > a + ul.submenu + ul.featured-content
). 也就是说,这是真正的实现细节,您可以针对您的用例进行优化。
在我进入代码之前,我应该注意到this is partially pseudocode 因为这个答案的一部分与这个问题没有直接的关系,所以我试图让一个已经很长的答案尽可能简洁。
class WPSE177330_Walker extends Walker_Nav_Menu {
// Don\'t need to modify start_lvl()
// Don\'t need to modify end_lvl()
// Don\'t (necessarily) need to modify start_el()
/**
* Outputs the closing for the element.
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Page data object. Not used.
* @param int $depth Depth of page. Not Used.
* @param array $args An array of arguments. @see wp_nav_menu()
*/
public function end_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
// Setup indent
$indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';
// Capture all top level items and append the featured content
if ( $depth < 1 ) {
if ( \'taxonomy\' == $item->type ) {
// Get your top posts in the category using $item info
// Assemble output
// Add it to output
$output .= $indent . $featured_content;
} else {
// What to do with top level menu items that aren\'t taxonomies
}
} else {
// What to do if depth is greater than 1
// Default behavior
$output .= "</li>\\n";
}
}
}
一定要弄得乱七八糟
$item
得到你想要的东西。这是
$item
从一个名为“Foo”的类别菜单项中,您可以了解要查找什么,以便准确定制所需的一切。
WP_Post Object
(
[ID] => 7
[post_author] => 1
[post_date] => 2015-02-07 15:29:42
[post_date_gmt] => 2015-02-07 20:29:42
[post_content] =>
[post_title] =>
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => 7
[to_ping] =>
[pinged] =>
[post_modified] => 2015-02-07 15:29:42
[post_modified_gmt] => 2015-02-07 20:29:42
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://sandbox.localhost/?p=7
[menu_order] => 1
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 7
[menu_item_parent] => 0
[object_id] => 3
[object] => category
[type] => taxonomy
[type_label] => Category
[url] => http://sandbox.localhost/category/foo/
[title] => Foo
[target] =>
[attr_title] =>
[description] =>
[classes] => Array
(
[0] =>
[1] => menu-item
[2] => menu-item-type-taxonomy
[3] => menu-item-object-category
)
[xfn] =>
[current] =>
[current_item_ancestor] =>
[current_item_parent] =>
)
我写这篇文章是为了避免使用小部件,正如你在评论中所说的那样。我觉得这是一个更干净的实现,因为它允许您只设置一次,而不会让您的小部件与一堆特定于菜单的小部件混在一起。如果您想改用小部件(正如您最初提议的那样),也许为了比硬编码解决方案更灵活一些,您可以为每个顶级菜单项注册小部件,然后使用相同的自定义概念
Walker_Nav_Menu
扩展以显示这些小部件。