WordPressのメニューシステムには、メニュー項目に説明を追加できるビルトイン機能があります。しかし、この機能は初期設定では非表示になっています。有効化されている場合でも、いくつかのコードを追加しない限り、説明文の表示はサポートされていません。ほとんどのテーマはメニュー項目の説明を考慮してデザインされていない。この投稿では、WordPressでメニューの説明を有効化する方法と、WordPressテーマでメニューの説明を追加する方法を紹介します。
注意: このチュートリアルでは、HTML、CSS、WordPressテーマ開発についての十分な理解が必須です。
メニューの説明を追加するタイミングと理由
メニューの説明を追加することがSEOに役立つと考えるユーザーもいます。しかし、私たちは、メニュー記述を追加する主な理由は、サイト上でより良いユーザー体験を提供するためだと考えています。
説明文は、訪問者がメニュー項目をクリックした場合に何が見つかるかを伝えるために使用することができます。魅力的な説明文は、メニューをクリックするユーザーをより惹きつけることでしょう。
ステップ1:メニューの説明をオンにする
外観 ” メニューに移動します。ページ右上の表示オプションボタンをクリックします。説明ボックスにチェックを入れる。
これでメニュー項目の説明欄が有効化されます。このように:
これでWordPressメニューの項目にメニューの説明を追加できるようになりました。しかし、これらの説明はまだテーマには表示されません。メニューの説明を表示するには、いくつかのコードを追加する必要があります。
ステップ 2: walker クラスを追加する:
ウォーカークラスはWordPressの既存のクラスを拡張する。基本的には、メニュー項目の説明を表示するためのコード行を追加するだけです。テーマのfunctions.php
ファイルにこのコードを追加する。
[cbk1]
ステップ3.wp_nav_menuでウォーカーを有効化する。
WordPress テーマでは、wp_nav_menu()関数を使用してメニューを表示します。WordPressテーマでカスタムナビゲーションメニューを追加する方法については、初心者向けのチュートリアルも公開しています。ほとんどのWordPressテーマは、header.php
テンプレートにメニューを追加します。しかし、あなたのテーマがメニューを表示するために他のテンプレートファイルを使用している可能性があります。
今必要なことは、あなたのテーマ(おそらくheader.php内)でwp_nav_menu()
関数を見つけ、次のように変更することです。
<?php $walker = new Menu_With_Description; ?> <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'walker' => $walker ) ); ?>
最初の行では、$walkerに
functions.phpで
定義したwalkerクラスを使うように設定しています。2行目のコードでは、既存の wp_nav_menu 引数に追加する引数は'walker' => $walker
だけです。
ステップ4.説明文のスタイリング
先ほど追加した walker クラスは、この行のコードで項目の説明を表示します:
[cbk3]
上記のコードでは、メニュー項目に改行を追加するために
タグを追加し、説明をクラスsubのspanで囲んでいます。こんな感じ:
[SKX1]
<li id="menu-item-99" class="menu-item menu-item-type-post_type menu-item-object-page"><a href="http://www.example.com/about/">About<br /><span class="sub">私たちは誰ですか</span></a></li><br /><span class="sub">私たちは誰ですか?
[SKX2]
サイト上での説明の外観を変更するには、テーマのスタイルシートにCSSを追加します。私たちはTwenty Twelveでこれをテストし、このCSSを使用しました。
[cbk4]
この投稿があなたのテーマでメニュー説明のあるクールなメニューを作成するのに役立つことを願っています。質問がありますか?下のコメントをしてください。
その他のリソース
ビル・エリクソンの説明付きメニュークラス
Matthew Blaxton
In PHP 8.0 and higher this with throw a critical error.
You need to find this line:
function start_el( $output, $item, $depth, $args ) {
Changing that line to the following should make the error disappear:
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
WPBeginner Support
Thank you for sharing that for those on PHP 8.
管理者
Norman
Hi,
How can you make the description clickable too?
WPBeginner Support
We cover that in step 4
管理者
kayvan A.Gilani
To Add Menu Descriptions in My WordPress Themes, I did step 1 and 2 of this blog but couldn’t follow in step 3 to move forward and perform the total change.
WPBeginner Support
If you cannot find the function in your theme, we would recommend reaching out to your specific theme’s support and they should be able to assist.
管理者
yiannis
Hi,
How to disable product category description in max mega menu ?
I have already gone to Mega Menu > General Settings and set Menu Item Descriptions to disabled but the problem exists.
WPBeginner Support
You would want to reach out to the plugin’s support and they would be able to assist with the setting not working correctly
管理者
Sergio
Thank you very much!
WPBeginner Support
You’re welcome
管理者
Lanka
Thank you so much, you saved me
JKLYN
Thanks. Saved my time.
dan
Can this work with the WP_Bootstrap_Navwalker ?
as i’ve tried it and it breaks my site….
kalpana
Thank you so much….was very useful to me….you saved my day
Steven
how to turn off the description in mobile layout?
Anzani Zahrani
Hello.. Please Help..
How to add title category, not using title category description ?
Ido Schacham
Totally helpful, thanks!
Rahman
Great Tip but in menu description it does not support html tags. can anyone know about this?
Thanks
Iryna
Hi Guys,
Any ideas how to allow html tags in the description?
remove_filter(‘nav_menu_description’, ‘strip_tags’);
this one not work for me.
Damien Carbery
@Iryna: Can you post your code somewhere e.g. pastebin.com.
Where you call remove_filter() will determine whether it works – it has to be called after the add_filter() call.
Calling it just before the wp_nav_menu() call might work.
Max
Is there anyway for the description not to be hyperlinked?
Ashok
thanks…it worked. but in menu description it does not support html tags.
igorasas
May already be there ready to plug-in? How this hack will work with the theme of “Twenty TwelveVersion: 1.5”
? And just as with the plugin wpml?
Guy
Thanks for the tip
Phong
Thank you, this was really helpful to just copy paste this and get the quick picture.
Chad
Hey man, I added the walker class to functions.php, but I cannot find the wp_nav_menu in genesis theme. What am I missing? I have no idea what to do next?!?!
Ksenia
You expain very well – detailed and clearly. 1 minute and menu items descriptions are ok. Thank you!
Mary Anne
Thank you so much for this tutorial. It was recommended to me and it worked perfectly for making the changes I wanted to make. However, in making these changes, I’ve lost my drop down sub-item menus. Any idea what affected that in the code change?
Thank you for your time and tutorial
WPBeginner Support
It seems like a CSS issue, we are sorry we couldn’t be more specific. Try using Google Chrome’s developer tools to debug it.
管理者
Paul Renault
I have implemented the menu descriptions and it worked great. Now my client is asking for a line break within one of the descriptions. I have tried putting a carriage return and inserting a tag into the description field through the admin. It doesn’t appear in the front end. WP removes these edits. Is there a way to remedy this?
WPBeginner Support
If your client just wants spacing then you can use CSS for that.
管理者
Paul Renault
The client wants a line break. Is there a way that I can insert a tag in the description? If I put one in now it gets removed.
Barry
Great tutorial guys, just want to know how to implement this on a custom menu displayed using the Custom Menu widget?
Oryan Consulting
Thank you! Been working on WordPress for years and I’ve never even heard of this before. I was looking to remove the descriptions as they were very redundant on the site I’m working on. I looked everywhere for where they were coming from.
Oh joy!
sambassador
works!
but for php 5.4 you’ll have to match the wp walker arguments for the start_el function:
function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 )
and find replace $item with $object.
Steve Covello
You Rock!! Worked perfectly.
Kevin Gilbert
Perfect. This was just what I needed to finish up on a site. I had some issues with the CSS, but I finally figured it out and got it working. Thanks for the great articles.
Jon
Excellent tip. Worked perfectly!
Jedediah White
This worked perfectly for me. The class function is perfect too. Thanks a lot!
Pankaj
I needed to create same thing and I was totally lost.
I was planning to do some stupid things to get this thing done.
thank god I found this post and saved time and stress!
I simply love this site got to know so much things.
Thanks you so much for showing the easiest things here.
Pankaj
The span tag is coming on sub-menus too.
its not showing there but it is taking that much of space which makes it look too odd.
is there any workaround for the same??
DiTesco
This is really a great tutorial and I was wondering if this would work on the Thesis 1.8.5? If not, it would be great if you can provide one. I will most certainly help you put it out there. Thumbs up!
svet
I followed your tutorial and added description to my menu. Thanks! However, when I am in mobile mode menu converts into dropdown menu and menu title and description are connected. For example, if my menu item is “about” and description “more about me”, the mobile version shows “aboutmore about me”. Is there a way to fix this?
David
I had the same problem. Here’s what I did.
I changed this:
$description = ! empty( $item->description ) ? ‘<span>’.esc_attr( $item->description ).'</span>’ : ”;
To this:
$description = ! empty( $item->description ) ? ‘<br /><span>’.esc_attr( $item->description ).'</span>’ : ”;
Not sure if it’s the best solution, but it worked for me.
Garrett Hyder
Thanks guys, I ran into what SVET and DAVID did with the mobile menu.
The code seems to have changed my change was simply appending in a span with the dash seperator and in my desktop query simply suppressed it as was unneeded there.
$item_output .= ‘ – ‘;
Within my Desktop Only Query set the span to display none;
@media only screen and (min-width: 740px) {
header #submenu li span.dash { display:none; }
Hope that helps, handled my issue nicely.
Nicola
Great post – very clear, exactly what I needed and worked perfectly. Thank you!
Samedi Amba
Thanks for the Great Tutorial. I’ve done the major steps well, as you can see from
http://ueab.ac.ke/demo/index
I was stuck with the styling-how do I reduce the space between the Main Menu Label and the description? Your help is greatly appreciated.
Editorial Staff
It has to do with the line-height of your .menu a class of your theme. If you reduce that, then the spacing will reduce itself.
管理者
Chris Rouse
Great post. I’ve tried to dig into this before but the previous instructions I found were not this easy to follow. I was able to drop the functions.php code in, figure out how to change the walker class in my header file (different for the theme I use, but straight forward), and get things going in about 15 minutes from start to finish.
One piece that you might want to add is how to include the right border on the last menu item using the :last-child property.
.menu-item:last-child {
border-right: 1px solid #ccc;
}
Damien Carbery
Instead of extending Walker_Nav_Menu it would be nice (and easier) if a filter was provided e.g.
If the core code had:
$item_output .= apply_filters( ‘walker_nav_menu_description’, $item->description);
Then the custom filter function would just have:
return ” . $description . ”;
Cathy Earle
Great info … going to start adding this to my sites. Thank you!