导航菜单中的WP_LIST_CATERIONS

时间:2014-02-10 作者:user3211948

我有一个网站,我想自动添加类别到我的菜单。我的意思是,当我创建新类别时,它应该出现在我的菜单中。我怎样才能做到?

  <?php wp_nav_menu( array( \'theme_location\' => \'header\', \'container\' => \'\' ) ); ?>

Home | Categories | Contact
          category1
          category2
          category3

2 个回复
SO网友:Will

至少应该从这些方面着手。您需要先注册菜单并获取ID。我没有删除类别,但您也需要注意这一点。

This is largely untested. Back up your DB first!!

add_action( \'created_category\', \'my_update_category_menu\' );
add_action( \'delete_category \', \'my_delete_category_menu\' );

function my_update_category_menu ($cat) {
    $category = get_category($cat);
    $menu_id = yourMenuIdHere; // <--------- YOUR ID

    wp_update_nav_menu_item($menu_id, 0, array(
        \'menu-item-title\' =>  __($category->name),
        \'menu-item-classes\' => $category->name,
        \'menu-item-url\' => home_url( get_category_link( $category->term_id ) ), 
        \'menu-item-status\' => \'publish\')
    );

}
function my_delete_category_menu ($cat) {
    $category = get_category($cat);
    // not sure how to get the backwards look up but menu items are stored as posts 
    // http://codex.wordpress.org/Function_Reference/wp_delete_post
}
如果您想批量添加现有类别(尽管通过管理员手动添加很容易),应该可以按照以下方式进行操作。运行一次并将其从函数中删除。php。

$categories = get_categories();

foreach($categories as $category) {

     wp_update_nav_menu_item($menu_id, 0, array(
        \'menu-item-title\' =>  __($category->name),
        \'menu-item-classes\' => $category->name,
        \'menu-item-url\' => home_url( get_category_link( $category->term_id ) ), 
        \'menu-item-status\' => \'publish\')
    );

}

SO网友:user3211948

我将其添加到函数中。php但什么都没发生

add_action( \'created_category\', \'my_update_category_menu\' );
add_action( \'delete_category \', \'my_delete_category_menu\' );

function my_update_category_menu ($cat) {
    $category = get_category($cat);
    $menu_id = \'menu-primary-menu\'; // <--------- YOUR ID

    wp_update_nav_menu_item($menu_id, 0, array(
        \'menu-item-title\' =>  __($category->name),
        \'menu-item-classes\' => $category->name,
        \'menu-item-url\' => home_url( get_category_link( $category->term_id ) ), 
        \'menu-item-status\' => \'publish\')
    );

}
function my_delete_category_menu ($cat) {
    $category = get_category($cat);
    // not sure how to get the backwards look up but menu items are stored as posts 
    // http://codex.wordpress.org/Function_Reference/wp_delete_post
}

结束

相关推荐

Completely disable categories

有人知道有一个插件在WP中禁用了类别吗?我的意思是,对于现有用户和新用户,完全自动地从视图中隐藏该功能。特别是:WP admin菜单、帖子列表、它的屏幕选项和过滤器、帖子编辑器和它的屏幕选项、widgets屏幕、writing和permalink设置屏幕我遇到了各种老化的黑客来完成这一部分工作,其中大部分是用来禁用元框的代码和一些用来禁用permalink前缀的插件。我之所以选择这样做,是希望一些网页设计师或开发人员能够掌握整套功能。如果没有,在过去的3-4年中,是否会有新的API使过程变得更简单(我的W