wp_nav_menu

Sponsored Link

ナビゲーションメニューを表示します。メニューはバックエンド:外観>メニューから作成する事ができます。

Sponsored Link

Display a navigation menu. Menus can be created from the backend: Appearance > Menus.

Given a theme_location parameter, the function displays the menu assigned to that location, or nothing if no such location exists or no menu is assigned to it.

If not given a theme_location parameter, the function displays

  • the menu matching the ID, slug, or name given by the menu parameter, if that menu has at least 1 item;
  • otherwise, the first non-empty menu;
  • otherwise, output of the function given by the fallback_cb parameter (wp_page_menu(), by default);
  • otherwise nothing.

wp_nav_menuのテンプレートタグ使用方法

<?php wp_nav_menu($args); ?> 

デフォルトの使い方

<?php $defaults = array(
  'theme_location'  => ,
  'menu'            => , 
  'container'       => 'div', 
  'container_class' => 'menu-{menu slug}-container', 
  'container_id'    => ,
  'menu_class'      => 'menu', 
  'menu_id'         => ,
  'echo'            => true,
  'fallback_cb'     => 'wp_page_menu',
  'before'          => ,
  'after'           => ,
  'link_before'     => ,
  'link_after'      => ,
  'items_wrap'      => '<ul id=\"%1$s\" class=\"%2$s\">%3$s</ul>',
  'depth'           => 0,
  'walker'          => );
?>

wp_nav_menuのパラメーター

$theme_locaton

(string) (optional) テーマ内のロケーションで使用されます。–ユーザーによって選択可能にするために、 register_nav_menu()で登録する必要があります。

(string) (optional) the location in the theme to be used–must be registered with register_nav_menu() in order to be selectable by the user

Default: None

$menu

(string) (optional) メニューは望まれていました;id、スラッグ、名前に対応します(順番に一致している)。

(string) (optional) The menu that is desired; accepts (matching in order) id, slug, name

Default: None

$container

(string) (optional) ulで囲むかどうか、何でそれを囲むか。

(string) (optional) Whether to wrap the ul, and what to wrap it with

Default: div

$container_class

(string) (optional)コンテナに適用されるクラスです。

(string) (optional) the class that is applied to the container

Default: menu-{menu slug}-container

$container_id

(string) (optional) コンテナに適用されるIDです。

(string) (optional) The ID that is applied to the container

Default: None

$menu_class

(string) (optional) CSSクラスは、メニューを形成するul要素に使用します。

(string) (optional) CSS class to use for the ul element which forms the menu

Default: menu

$menu_id

(string) (optional) メニューを形成するUL要素に適用されているIDです。

(string) (optional) The ID that is applied to the ul element which forms the menu

Default: menu slug, incremented

$echo

(boolean) (optional) メニューをエコーするか返すかどうかです。

(boolean) (optional) Whether to echo the menu or return it

Default: true

$fallback_cb

(string) (optional) メニューが存在しない場合は、コールバック関数を使用します。

(string) (optional) If the menu doesn’t exists, the callback function to use

Default: wp_page_menu

$before

(string) (optional) リンクテキストの前にテキストを出力します。

(string) (optional) Output text before the link text

Default: None

$after

(string) (optional) リンクテキストの後にテキストを出力します。

(string) (optional) Output text after the link text

Default: None

$link_before

(string) (optional) リンク前にテキストを出力します。

(string) (optional) Output text before the link

Default: None

$link_after

(string) (optional) リンク後にテキストを出力します。

(string) (optional) Output text after the link

Default: None

$items_wrap

(string) (optional) Whatever to wrap the items with an ul, and how to wrap them with

Default: None

$depth

(integer) (optional) いくつ階層のレベルは、含まれているか。0は全てを意味してます。

(integer) (optional) how many levels of the hierarchy are to be included where 0 means all

Default: 0

$walker

(string) (optional) カスタムウォーカーを使用します。

(string) (optional) Custom walker to use

Default: None

デフォルトの例

<div class="access">
 <?php wp_nav_menu(); ?>
</div>

特定のメニューをターゲット

<?php wp_nav_menu( array('menu' => 'Project Nav' )); ?>

Twenty Tenテーマでの使用方法

<div id="access" role="navigation">

    <?php /*

    Allow screen readers / text browsers to skip the navigation menu and
    get right to the good stuff. */ ?>

    <div class="skip-link screen-reader-text">
        <a href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentyten' ); ?>">
        <?php _e( 'Skip to content', 'twentyten' ); ?></a>
    </div>

    <?php /*

    Our navigation menu.  If one isn't filled out, wp_nav_menu falls
    back to wp_page_menu.  The menu assigned to the primary position is
    the one used.  If none is assigned, the menu with the lowest ID is
    used. */

    wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

</div><!-- #access -->

ナビゲーションコンテナを削除する方法

ナビゲーションコンテナを削除するために、
functions.phpで指定されたテーマの場所と関数の引数間で使用したwp_nav_menu(例:’theme_location’ => ‘primary-menu’)は、管理でそれに割当てられたメニューを持つ必要があります。
他の引数’container’ => ‘false’は無視されます。

In order to remove navigation container, theme location specified in functions.php and used among arguments in function wp_nav_menu ( eg. ‘theme_location’ => ‘primary-menu’ ) must have a menu assigned to it in administration! Othervise argument ‘container’ => ‘false’ is ignored.

<?php
function my_wp_nav_menu_args( $args = '' )
{
	$args&#91;'container'&#93; = false;
	return $args;
} // function

add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
?>

あるいは、

<?php wp_nav_menu( array( 'container' => '' ) ); ?>

リストアイテムを囲うULタグを削除する方法

この例は、リストアイテム周りのULタグを削除します。

This example will remove the ul around the list items.

<?php wp_nav_menu( array( 'items_wrap' => '%3$s' ) ); ?>

ログインユーザごとに異なるメニューを表示する方法

この例は、ログインしているユーザーとログインしていないユーザーによって、異なるメニューを表示するケースです。

This example would cause a menu to show for logged-in users and a different menu for users not logged-in.

<?php
if ( is_user_logged_in() ) {
     wp_nav_menu( array( 'theme_location' => 'logged-in-menu' ) );
} else {
     wp_nav_menu( array( 'theme_location' => 'logged-out-menu' ) );
}
?>

変更ログ

Since 3.0

ソースファイル

wp_nav_menu() は、wp-includes/nav-menu-template.php内に位置しています。

wp_nav_menu() is located in wp-includes/nav-menu-template.php.

リソース

Sponsored Link