是否可以创建一个具有类别的菜单,以便每次我都有一个子类别自动显示在菜单上?
据我所知,如果我有一个带有“水果”类别的菜单,并且我创建了子类别“香蕉”,我必须手动添加它。
是否可以创建一个具有类别的菜单,以便每次我都有一个子类别自动显示在菜单上?
据我所知,如果我有一个带有“水果”类别的菜单,并且我创建了子类别“香蕉”,我必须手动添加它。
这取决于您所谈论的菜单类型:
使用动作挂钩创建新功能add_category
在该功能中,您可以创建一个新的帖子类型menuitem类型,该类型会正确添加到菜单中
每当发布新类别时,都会立即添加菜单项
您还应该连接到“删除”类别,以使菜单保持最新
如果没有wordpress菜单,您可以做得更简单:
只需编写一个函数来列出您的类别(或使用预定义的wp_list_categories
函数)
我更喜欢使用wp_list_categories
. 以下是我在上一个项目中使用的代码:
<?php wp_list_categories(\'orderby=ID&exclude=3,10,1,16,38&title_li=<span class="sidebar_heading d_shadow">\' . __(\'Categories\') . \'</span>\'); ?>
您将获得所有类别和子类别的列表。经过多次研究,我找到了一个可靠而有用的解决方案,这门课效果很好:
class EntexAutoSubmenu {
/**
* Constructor
*/
function __construct(){
add_action(\'created_category\', array($this, \'on_publish_category\'));
}
/**
* When publishing a new child category, add it to the appropriate custom menu.
*/
function on_publish_category($cat_id){
// Theme supports custom menus?
if (!current_theme_supports(\'menus\')) return;
// Published taxonomy has parent?
$cat = get_category($cat_id);
if (!$cat->category_parent) return;
$all_menus = get_terms(\'nav_menu\', array(\'hide_empty\' => true));
// Loop through the menus to find page parent
foreach ($all_menus as $menu) {
$menu_parent = NULL;
$menu_items = wp_get_nav_menu_items($menu->term_id, array(\'post_status\' => \'publish,draft\'));
if (!is_array($menu_items)) continue;
foreach ($menu_items as $menu_item){
// Item already in menu?
if ($menu_item->object_id == $cat_id) continue 2;
if ($menu_item->object_id == $cat->category_parent) $menu_parent = $menu_item;
}
// Add new item
if ($menu_parent) {
wp_update_nav_menu_item($menu->term_id, 0, array(
\'menu-item-object-id\' => $cat_id,
\'menu-item-object\' => $cat->taxonomy,
\'menu-item-parent-id\' => $menu_parent->ID,
\'menu-item-type\' => \'taxonomy\',
\'menu-item-status\' => \'publish\'
));
}
}
}
}
$auto_submenu = new EntexAutoSubmenu();
另一种选择是使用List Custom Taxonomy Widget, 它可能适用于您的用例(如果您不打算使用WP nav\\U菜单)。
我为我的一个客户编写了自己的代码
<div class="menu">
<ul id="MenuBar1" class="MenuBarHorizontal">
<li><a <?PHP if( count($_GET) == 0 ){ echo \'class="-current"\';} ?> href="<?php echo esc_url( home_url( \'/\' ) ); ?>">home</a></li>
<?php
$category_ids = get_all_category_ids();
sort($category_ids);
foreach($category_ids as $cat_id) {
$cat_name = get_cat_name($cat_id);
echo \'<li><a href="\' . get_category_link( $cat_id ) . \'">\' . $cat_name . \'</a>\';
$args = array( \'numberposts\' => -1, \'offset\'=> 0, \'cat\' => $cat_id, \'orderby\' => \'ID\', \'order\' => \'ASCE\' );
echo \'<ul>\';
/* The 2nd Query (without global var) */
$query2 = new WP_Query( $args );
// The 2nd Loop
while( $query2->have_posts() ):
$query2->next_post();
echo \'<li><a href="\' . get_permalink( $query2->post->ID ) . \'">\' . get_the_title( $query2->post->ID ) . \'</a></li>\';
endwhile;
// Restore original Query & Post Data
wp_reset_query();
wp_reset_postdata();
echo \'</ul>\';
echo \'</li>\';
}
?>
</ul>
</div>
为了解决这个问题,我尝试创建一个插件,如果在菜单中,它会自动为父类别下的发布类别创建菜单项。
插件理念:
1.在代码中,我使用"created_$taxonomy" 行动挂钩。
2.收集所有菜单项(使用wp_get_nav_menu_items)
3.如果发布类别的父级位于菜单项中,而不是创建为其父类别的子菜单的菜单项中,则放置条件。(通过使用wp_update_nav_menu_item)
<?php
/*
Plugin Name: Auto Category Submenu
Plugin URI:
Description: Create menu item when category publish/create if it\'s parent category in menu item.
Version: 0.1
Author: Mayank Gupta/Ravinder Kumar
Author URI:
License: GPL2
*/
class AutoSubmenu {
/**
* Constructor
*/
function __construct() {
add_action( \'created_category\', array( &$this, \'on_publish_cat\' ) );
}
/**
* When publishing a new child page, add it to the appropriate custom menu.
*/
function on_publish_cat( $cat_id ) {
$cat = get_category( $cat_id );
//menu id for you menus in which your sub-category\'s parent category as menu item
$menu_ids = array(101,100);
//retrieve all meu items from menu
foreach($menu_ids as $menu_id){
$menu_items[] = wp_get_nav_menu_items( $menu_id, array(\'post_status\' => \'publish\',\'post_type\' => \'nav_menu_item\',\'output\' => ARRAY_A, ) );
}
foreach($menu_items as $menu_item){
$counter = 0 ;
echo\'<!--\';
print_r($menu_item);
echo\'-->\';
foreach($menu_item as $menu_term){
$menu_terms_id[] = $menu_term->object_id; //all menu items id
}
}
//print_r($menu_terms_id); //test for items in menu
foreach( $menu_terms_id as $menu_term ){
if( $cat->category_parent == $menu_term){
wp_update_nav_menu_item( $cat_id, 0, array(
\'menu-item-object-id\' => $cat_id,
\'menu-item-object\' => $cat->taxonomy,
\'menu-item-parent-id\' => $menu_term,
\'menu-item-type\' => \'category\',
\'menu-item-status\' => \'publish\'
) );
}else{
return;
}
}
}
}
$auto_submenu = new AutoSubmenu();
Note: plugin giving error (header already sent) and menu item not created when category publish.This may be another topic for problem but i put it here because this may be a solution if some suggest me where i am wrong.Sorry if i\'m doing something wrong by putting it here我正在使用在我的项目中将所有类别和类别的子类别显示为菜单。
$all_categories = get_categories( $args );
echo "<div class=\'container\'>";
echo "<div class=\'productsubmenu\'>";
echo "<ul>";
foreach ($all_categories as $cat)
{
if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, \'thumbnail_id\', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo \'<li class="parent \'.$my.\' "><a href="\'.get_term_link( $cat ) . \'">\'.$cat->name.\'</a>\';
$args2 = array(
\'taxonomy\' => $taxonomy,
\'child_of\' => 0,
\'parent\' => $category_id,
\'orderby\' => $orderby,
\'show_count\' => $show_count,
\'pad_counts\' => $pad_counts,
\'hierarchical\' => $hierarchical,
\'title_li\' => $title,
\'hide_empty\' => $empty
);
$args = array(
\'hierarchical\' => 1,
\'show_option_none\' => \'\',
\'hide_empty\' => 0,
\'parent\' => $parent_cat_ID,
\'taxonomy\' => \'product_cat\'
);
$subcats = get_categories($args2);
echo \'<ul class="wooc_sclist">\';
foreach ($subcats as $sc) {
}
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo \'<li class="\'.$my.\'" ><a href="\'. $link .\'">\'.$sc->name.\'</a></li>\';
}
echo \'</ul>\';
echo "</li>";
}
else
{
}
}
echo "</ul>";
echo "</div>";
echo "</div>";
这是我的方法,适用于产品类别。
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0):
echo $cat->name;
foreach ($cats as $key => $cat2):
if ($cat2->parent == $cat->term_id):
echo $cat2->name;
foreach ($cats as $key => $cat3):
if ($cat3->parent == $cat2->term_id):
echo $cat3->name;
endif;
endforeach;
endif;
endforeach;
endif;
endforeach;
我的问题是wordpress没有将两个菜单显示为单独的菜单,它只是将菜单上的内容输出到两个位置。URL:http://www.msc-media.co.uk检查页眉和页脚的链接-我知道页脚很难看,以后需要整理css。我在函数中创建了两个菜单。php使用此代码。<?php register_nav_menus( array( \'topmenu\' => \'Top Menu\', \'footermenu\' => \'Fo