编辑:我尝试了以下内容,它的主题是“woocommerce店面”(尽管它需要额外的样式)。然而,出于某种原因,它与“215”主题不太匹配。(子菜单不显示。)
add_filter( \'wp_nav_menu_items\', \'wc_products_on_nav_menu\'); //filter to add the html
add_action( \'save_post_product\', \'rebuild_custom_wc_nav_menu\'); //action to regenerate the menu array anytime a product is added or updated
function build_wc_extra_menu(){
//get the product categories
$args = array(
\'taxonomy\' => \'product_cat\',
\'orderby\' => \'name\',
\'show_count\' => false,
\'pad_counts\' => false,
\'hierarchical\' => true,
\'title_li\' => \'\',
\'hide_empty\' => false
);
$product_categories = get_categories( $args );
if(!empty($product_categories)){
//get the products in each category
foreach ($product_categories as $category){
$cat_menu = array();
$product_arg = array (
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'order\'=> \'ASC\',
\'product_cat\' => $category->slug,
);
$products = get_posts($product_arg);
foreach ($products as $product){
//store the products as an associtive array with the name as key and link as value(will cause conflicts if two products have the same title)
$link = get_post_permalink($product->ID);
$cat_menu[$product->post_title] = $link;
}
//store the array of products as the value of an array with the key as the name of the category
$custom_menu[$category->name] = $cat_menu;
}
}
//save the entire array in the wp_options table
update_option(\'wc_cat_menu\', $custom_menu);
}
function rebuild_custom_wc_nav_menu(){
//simply rebuild the array
build_wc_extra_menu();
}
function wc_products_on_nav_menu($menu) {
//check if the option exists, if not, rebuild the array
if(empty(get_option(\'wc_cat_menu\'))) build_wc_extra_menu();
//retrieve the array from the wp_options table
$custom_menu = get_option(\'wc_cat_menu\');
foreach ($custom_menu as $cat => $product_array){
//build the html to append to the menu
$cat_menu = \'<li class="page_item">\'.$cat.\'<ul class="sub-menu">\';
foreach ($product_array as $name => $link){
$product_submenu = \'<li class="page_item"><a href="\'.$link.\'">\'.$name.\'</a></li>\';
$cat_menu = $cat_menu.$product_submenu;
}
$cat_menu = $cat_menu.\'</ul></li>\';
//append the html to the menu
$menu = $menu.$cat_menu;
}
//return the filtered menu
return $menu;
}
编辑:下面的代码是原始代码。不要用这个!!!我把它留在这里是因为有评论。
试试这个。我没有运行代码,所以请尝试一下,如果可行,请发表评论:)
add_filter( \'wp_nav_menu_items\', \'wc_products_on_nav_menu\');
//the filter hook to edit the nav_menu list
function wc_products_on_nav_menu($menu) {
//first get all the product categories
$product_categories = get_terms( array(
\'taxonomy\' => \'product_cat\',
\'hide_empty\' => true,
) );
if(!empty($product_categories)){
foreach ($product_categories as $category){
//add a top level list for the category name.
$cat_menu = \'<li>\'.$category->name.\'<ul>\';
$product_arg = array (
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'order\'=> \'ASC\',
\'product_cat\' => $category->slug,
);
$products = get_posts($product_arg);
foreach ($products as $product){
//add the products in that category as sub menu items
$link = get_post_permalink($product->ID);
$menu_item = \'<li><a href="\'.$link.\'" >\'.$product->post_title.\'</a></li>\';
$cat_menu = $cat_menu . $menu_item;
}
$cat_menu = $cat_menu.\'</ul></li>\';
$menu = $menu.$cat_menu;
}
}
return $menu;
}