我需要使用 woo commerce 产品类别在 wordpress 中添加菜单吗? [英] I need to add menu in wordpress using woo commerce product category?

查看:16
本文介绍了我需要使用 woo commerce 产品类别在 wordpress 中添加菜单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我为菜单创建了一个代码.它提供了一个菜单,但无法添加产品类别.将不胜感激.谢谢.

Here I am created a code for menu.It gives an menu but cann't add product category. Help will be appreciated.thank you.

 // Filter wp_nav_menu() to add additional links and other output          function new_nav_menu_items($items) {
    $homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
    // add the home link to the end of the menu
    $items = $items . $homelink;
    return $items;
    }

add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

推荐答案

首先你需要在 wordpress admin menu 空白菜单中创建菜单.现在转到function.php文件(主题文件)中添加以下代码.

First you need to create menu in word press admin menu blank menu. Now go to function.php file (theme file) add following code in it.

您可以通过此功能获取产品类别列表,

You can get products cateogorty list from this function,

function get_product_terms( $term_id ) {

        $html = '';

        $args = array( 'hide_empty' => 0, 'parent' => $term_id );

        $terms = get_terms('product_cat', $args);

        foreach ($terms as $term) {

            $html .= '<li';

            if( $term_id == 0 ) {

                $html .= ' class="top_li"';

            }

            $html .= '><a href="'.get_term_link($term->slug, 'product_cat').'">' . $term->name . '</a>';    

            if( $list = get_product_terms( $term->term_id )) {

                $html .= '<ul class="second_level">'.$list.'</ul>';

            }

            $html .= '</li>';

        }

        return $html;

    }

您可以使用此功能将产品类别添加到菜单中,

You can add products category to menu using this function,

// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
    // Woo function

    /*//product_cat
    $terms = get_terms( 'product_cat', $args );
    print_r($terms);*/
    if( $list = get_product_terms( 0 )) {



    $menu1link = '<li class="home"><a href="' . home_url( '/' ) . '">' . __($list) . '</a></li>';
    $homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
    // add the home link to the end of the menu
    $items = $items . $homelink;
    $items = $items .$menu1link;
    }
    return $items;

}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

这篇关于我需要使用 woo commerce 产品类别在 wordpress 中添加菜单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆