register_nav_menus() - WordPress 函数参考中文文档

返回介绍

register_nav_menus()

发布于 2017-09-11 字数3194 浏览 1080 评论 0

register_nav_menus( array $locations = array() )

Registers navigation menu locations for a theme.


描述


参数

$locations

(array)
(Optional)
Associative array of menu location identifiers (like a slug) and descriptive text.

Default value: array()


源代码

File: wp-includes/nav-menu.php

function register_nav_menus( $locations = array() ) {
	global $_wp_registered_nav_menus;

	add_theme_support( 'menus' );

	$_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
}

更新日志

Version 描述
3.0.0 Introduced.

相关函数

Uses

  • wp-includes/theme.php:
    add_theme_support()

Used By

  • wp-includes/nav-menu.php:
    register_nav_menu()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example

    
    register_nav_menus( array(
    	'pluginbuddy_mobile' => 'PluginBuddy Mobile Navigation Menu',
    	'footer_menu' => 'My Custom Footer Menu',
    ) );
    
    
  2. Creating menus from your Custom Taxonomies.

    For example: I have a custom taxonomy named ‘Country’ with a few countries in list.
    Right now I want to assign each country has a private name and using it for condition displayed on frontend.

    “` $tax = ‘country-category’;

    $terms = get_terms( $tax, [
    ‘hide_empty’ => false,
    ]);

    $args = array(
    ‘primary’ => __( ‘Primary Menu’, ‘khoipro’ ),
    ‘secondary => __( ‘Secondary Menu’, ‘khoipro’ )
    );

    // Loop through all terms to add term id as menu id and term name as menu name.
    foreach( $terms as $term) {
    $args = array_merge($args, array(
    ‘primary_’.$term->slug => ‘Country Menu (‘.$term->name.’)’
    ));
    }

    register_nav_menus( $args );
    “`

    So my output codes should be displayed in Menus > Manage Locations:

    * Primary Menu (id: primary)
    * Secondary Menu (id: secondary)
    * Country Menu (Japan) (id: primary_japan)
    * Country Menu (Singapore) (id: primary_singapore)
    * Country menu (Vietnam) (id: primary_vietnam)

    Have a proof in a real practice with a newly created project.

上一篇:register_nav_menu()

下一篇:register_new_user()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!