如何更改菜单项的位置?

时间:2015-07-15 作者:Roland Allla

如何更改产品菜单的位置?我使用下面的代码通过硬编码创建了它。

enter image description here

add_filter( \'the_content\', \'filter_ptags_on_images\' );
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>\';        
    return $items;
}

2 个回复
最合适的回答,由SO网友:Bryan Willis 整理而成

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/ 这可以帮助你实现你的目标,而且都是免费的。

SO网友:Nathan Wright

在菜单中添加项目的有趣方式。您是否对菜单的其余部分使用外观>菜单?如果不是,你应该是IMO。

我建议使用uber menu 要做到这一点,插件是值得的,它的重量是金子(不是附属的,只是使用过它并强烈推荐)-在优步菜单中,您可以向菜单中添加动态内容,如术语-请参阅本教程:http://sevenspark.com/docs/ubermenu-3/advanced-menu-items/dynamic-terms

结束

相关推荐