Ubermenu很棒,但如果这就是你想要做的,那么可能就没有必要了。有很多不同的方法可以做到这一点。这里有几个:
<小时>
1) Custom Walker
<?php
// custom walker targeting products menu
class productMenu extends Walker_Nav_Menu {
function end_el(&$output, $item, $depth=0, $args=array()) {
if( \'Products\' == $item->title ){
// PRODUCT SUBMENU STUFF
$output .= \'<ul class="sub-menu">
</ul>\';
}
$output .= "</li>\\n";
}
}
// new menu with walker
wp_nav_menu(
array(
\'theme_location\' => \'primary\',
\'walker\' => new productMenu
)
);
2) Just add the ones you want after products to the filter as well
add_filter( \'wp_nav_menu_items\', \'add_my_terms\', 10, 2 );
function add_my_terms( $items ) {
global $post;
$items .= \'<li class="dropdown "><a class="dropdown-toggle " data-toggle="dropdown" href="#">Products</a><ul class="dropdown-menu coldrpprd ">\';
$terms = get_terms( \'product_cat\', $args );
$nmr=0;
$args = array(
\'hierarchical\' => 1,
\'show_option_none\' => \'\',
\'hide_empty\' => 0,
\'parent\' => $term->term_id,
\'taxonomy\' => \'product_cat\'
);
$subcats = get_terms( \'product_cat\', $args );
foreach ( $terms as $term ) {
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, \'thumbnail_id\', true);
$image = wp_get_attachment_url($category_thumbnail);
$nmr++ ;
if ( $nmr <= 3 ) {
$items .= \'<li class="menuli">\'.\'<img class="category-image " src="\'.$image.\'">\'.\'<a href="\' . get_term_link($term) . \'">\' . $term->name . \'</a></li>\';
$items .= \'<ul>\';
foreach ( $subcats as $sc ) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
$items .= \'<li class="wooc_sclist"><a href="\'. $link .\'">\'.$sc->name.\'</a></li>\';
}
$items .= \'</ul>\';
}
}
$items .= \'</ul></li><li><a href="#">Products</a></li>\';
return $items;
return
}
3) Just use your function without wp_nav_menu() and hardcode it all. Is it really necessary to have to have those menus editable in the backend? If not just do this: <ul>
<li>Servizi</li>
<?php echo add_my_terms(); ?>
<li>Download</li>
<li>Contatti</li>
<li>Pricacy</li>
</ul>
4) If you really want to make this work without walker you could probably use wp_get_nav_menu_items()
and search for product menu item by id and add submenu, but this is unnecessarily complicated for your task in my opinion. 一
<ul>
<li>Servizi</li>
<?php echo add_my_terms(); ?>
<li>Download</li>
<li>Contatti</li>
<li>Pricacy</li>
</ul>
6) Use shortcodes. // Turn function into shortcode
function add_my_terms( $items ) {
global $post;
$items .= \'<ul class="dropdown-menu coldrpprd ">\';
$terms = get_terms( \'product_cat\', $args );
$nmr=0;
$args = array(
\'hierarchical\' => 1,
\'show_option_none\' => \'\',
\'hide_empty\' => 0,
\'parent\' => $term->term_id,
\'taxonomy\' => \'product_cat\'
);
$subcats = get_terms( \'product_cat\', $args );
foreach ( $terms as $term ) {
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, \'thumbnail_id\', true);
$image = wp_get_attachment_url($category_thumbnail);
$nmr++ ;
if ( $nmr <= 3 ) {
$items .= \'<li class="menuli">\'.\'<img class="category-image " src="\'.$image.\'">\'.\'<a href="\' . get_term_link($term) . \'">\' . $term->name . \'</a></li>\';
$items .= \'<ul>\';
foreach ( $subcats as $sc ) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
$items .= \'<li class="wooc_sclist"><a href="\'. $link .\'">\'.$sc->name.\'</a></li>\';
}
$items .= \'</ul>\';
}
}
$items .= \'</ul>\';
return $items;
}
add_shortcode(\'product_submenu\', \'add_my_terms\');
// Allow shortcodes to render in menu description fields
add_filter(\'wp_nav_menu_items\', \'do_shortcode\');
add_filter(\'wp_nav_menu\', \'do_shortcode\', 11);
if( !function_exists(\'shortcode_my_menu_descriptions\') ) {
function shortcode_my_menu_descriptions( $item_output, $item ) {
if ( !empty($item->description)) {
$output = do_shortcode($item->description);
if ( $output != $item->description )
$item_output = $output;
}
return $item_output;
}
add_filter("walker_nav_menu_start_el", "shortcode_my_menu_descriptions" , 10 , 2);
}
使用此方法,您可以按照所需的顺序正常创建菜单,然后只需添加[product_submenu]
在product(产品)菜单项的description(说明)字段中。7) Using plugins
ubermenu旁边还有https://wordpress.org/plugins/megamenu/ 和https://wordpress.org/plugins/shortcode-in-menus/ 这可以帮助你实现你的目标,而且都是免费的。