Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressで親ページの子ページ一覧を表示する方法

最近、読者から「WordPressのページの子ページを表示するにはどうしたらいいですか?

WordPressサイトを親ページと子ページで構成している場合、メインの親ページに子ページやサブページを表示させたいと思うかもしれません。また、各サブページにメインページを表示して、閲覧しやすくすることもできます。

この投稿では、WordPressで親ページの子ページ一覧を簡単に表示する方法を紹介します。

Displaying a list of child pages for a parent page in WordPress

子ページのリストを表示する必要がある場合は?

WordPressには、投稿とページと呼ばれる2つの初期投稿タイプが用意されています。投稿はブログのコンテンツで、通常カテゴリーとタグで構成されます。

ページとは、例えば「会社概要」ページや「お問い合わせ」ページのような、常時更新される単発または独立したコンテンツのことです。

WordPressではページを階層化することができ、親ページと子ページで整理することができます。

例えば、製品ページを作成し、機能、価格、サポートの子ページを作成したいとします。

子ページを作成するには、WordPressで子ページを作成する方法のガイドに従ってください。

親ページと子ページを作成した後、メインの親ページに子ページを掲載したいと思うかもしれません。

これを行う簡単な方法は、親ページを手動で編集し、リンクのリストを個別に追加することである。

Manually add child page links

しかし、子ページを追加したり削除したりするたびに、親ページを手動で編集する必要があります。子ページを作成するだけで、自動的に親ページのリンクとして表示されるのであれば、もっといいのではないでしょうか?

ということで、WordPressで親ページに子ページのリストを素早く表示する他のダイナミックな方法を見てみましょう。3つの方法を紹介するので、自分に合ったものを選んでほしい:

方法1.プラグインを使って親ページに子ページを表示する。

この方法は簡単で、すべてのユーザーにお勧めできる。

まず、Page-listプラグインをインストールして有効化する必要があります。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

有効化したら、親ページを編集し、子ページのリストを表示したい場所に以下のショートコードを追加するだけです。

[サブページ]

ページを保存し、新しいブラウザータブでプレビューすることができます。すべての子ページの箇条書きリストが表示されます。

Plain list of child page links

必要であれば、カスタムCSSを追加してリストの外観を変更することもできます。以下は、手始めに使えるCSSのサンプルです。

ul.page-list.subpages-page-list {
    list-style: none;
    list-style-type: none;
    background-color: #eee;
    border: 1px solid #CCC;
    padding: 20px;
}

カスタムCSSを適用したら、親ページをプレビューできます。WordPressのテストサイトではこのように表示されました。

Child pages list with CSS

このプラグインは、深さ、除外ページ、項目数などを設定できるショートコードパラメータを多数プロバイダーしています。詳しくはプラグインのページで詳しいドキュメンテーションをご覧ください。

方法2.コードを使って親ページの子ページをリストアップする

この方法は少し高度で、WordPressサイトにコードを追加する必要があります。WordPressでコードをコピー&ペーストする方法をご覧ください。

親ページの下に子ページを表示するには、コード・スニペット・プラグインまたはテーマのfunctions.phpファイルに以下のコードを追加する必要があります:

function wpb_list_child_pages() { 
 
global $post; 
 
if ( is_page() && $post->post_parent )
 
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
 
if ( $childpages ) {
 
    $string = '<ul class="wpb_page_list">' . $childpages . '</ul>';
}
 
return $string;
 
}
 
add_shortcode('wpb_childpages', 'wpb_list_child_pages');

WPBeginnerでは、常にWPCodeプラグインを使ってWordPressにコードを追加することを推奨しています。

WPCodeを使えば、テーマファイルを編集することなく簡単にカスタマイザーコードを追加できるので、サイトを壊す心配がありません。

WPCode

まず、無料のWPCodeプラグインをインストールし、有効化する必要があります。ステップバイステップの手順は、WordPressプラグインのインストール方法をご覧ください。

プラグインを有効化したら、WordPressダッシュボードからCode Snippets ” Add Snippetに移動します。そこから、’カスタムコードを追加(新規スニペット)’オプションにマウスオーバーし、’スニペットを使用’ボタンをクリックします。

Add a new custom code snippet in WPCode

次に、スニペットのタイトルを追加し、上記のコードを「コードプレビュー」ボックスに貼り付けます。

右側のドロップダウンメニューから、コードタイプとして「PHP Snippet」を選択することを忘れないでください。

Paste code into WPCode plugin

その後、スイッチを「非有効化」から「有効化」に切り替え、ページ上部の「スニペットを保存」ボタンをクリックするだけです。

Activate and save your custom code snippet

このコードはまず、ページに親がいるか、ページ自体が親であるかをチェックする。

親ページであれば、それに関連する子ページを表示する。子ページであれば、親ページの子ページをすべて表示します。

最後に、もしこのページが子ページも親ページもないただのページであれば、コードは単に何もしません。コードの最後の行では、ショートコードを追加しているので、ページテンプレートを変更することなく、簡単に子ページを表示することができます。

子ページを表示するには、サイドバーのページまたはテキストウィジェットに以下のショートコードを追加するだけです:

[wpb_childpages]。

変更を保存し、ブラウザータブでプレビューすることをお忘れなく。これがテストサイトでの外観です。

Plain link list

カスタムCSSを使用して、このページリストをスタイル設定することができます。以下は、CSSのサンプルコードです。

ul.wpb_page_list {
    list-style: none;
    list-style-type: none;
    background-color: #eee;
    border: 1px solid #CCC;
    padding: 20px;
}

方法3.ショートコードなしで子ページを動的に表示する

ショートコードを使うのは便利だが、親ページや子ページを持つすべてのページにショートコードを追加しなければならないという問題がある。

たくさんのページにショートコードを入れてしまい、時にはショートコードを入れ忘れることもあるだろう。

より良い方法は、テーマのページテンプレートファイルを編集して、自動的に子ページを表示できるようにすることだ。

そのためには、mainpage.phpテンプレートを編集するか、テーマでカスタマイザーページテンプレートを作成する必要があります。

メインテーマを編集することはできますが、テーマを変更したり更新したりすると、その変更は消えてしまいます。そのため、子テーマを作成し、その子テーマで変更を加える方がよいでしょう。

ページ・テンプレート・ファイルに、子ページを表示したい場所に次のコードを追加する必要があります。

<?php wpb_list_child_pages(); ?>

これですべてです。これであなたのテーマは自動的に子ページを検出し、プレーンなリストに表示します。

CSSとフォーマットでスタイルをカスタマイズすることができます。以下は、OptinMonsterサイトが親ページとサブページをどのように表示するかの例です:

OptinMonster Sub Pages Example

この投稿がWordPressで親ページの子ページをリストアップするのにお役に立てば幸いです。また、新規WordPressサイトで作成すべき最も重要なページに関するガイドや、コードなしでカスタム・レイアウトを作成するための最高のドラッグ&ドロップWordPressページビルダーの比較もご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

アバター

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

81件のコメント返信を残す

  1. Syed Balkhi

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Denise

    For third level pages (grandchild), I want to show the same menu that is seen on the child pages (all the child links of the parent). With this snippet, when on a grandchild page, I only see the other grandchild pages in the menu. How would this code need to be modified to show the all child links even when on grandchild pages?

  3. vicky

    how to get child page ids not the granchild ids…………….pls help im a bigginner

  4. Meredith L

    I’m relatively new to blogging and I recently switched over to the Kale WordPress theme. I have been trying to do page attribute pages (under the parent page Recipes) so I can have separate categories for appetizers, desserts, etc. Everything seems to go through on the admin part but then on the site itself there are no page attributes/drop down form the Recipes category. What am I doing wrong? It is all set to public so I’m not sure what the problem is.

  5. Daves

    Thank you for this code. But I don’t want this to be shown on the homepage (static page), just on the parent pages only. Please how can it be done?

    Thank you.

  6. Boris Budeck

    I am using Generatepress Theme (created a child theme from it for customizing) and I can’t get this to work. The shortcode is displayed in the output page, it seems it is not even ercognized as being a shortcode. I use Elementor widgets and neither text nor shortcode widgets work.

    Any idea of how to make it work when using elementor widgets?

  7. Frederic

    a great post, it’s exactly what I’m looking for since many hours ! and it works like a charm, just adding the code in the function.php of the theme then adding the short code in the page where I want the sub pages to appear, and voilà!!!
    wonderful, thanks a lot for your generosity !!

  8. Mariano

    Hi!

    Thanks for the code. How can I display the featured image of each child page?

  9. Ron

    Like with so many of the snippets we find, one has to go through all the comments in order to get it to work. What’s wrong with testing what you write?

  10. Alicia

    Can you do a drop down menu for parent/child pages on wordpress.com, or does it have to be the .org version?

  11. Razvan

    What if I want to display child posts, not child pages?

  12. Rudy

    Using this code ends up displaying the parent page along with the child pages, which is redundant. As I understand it, what we really need is to display only the child pages, not the parent page. Any suggested modification that can do this?

  13. Milos

    On plugins update, for some strange reason I always get an error:

    Fatal error: Cannot redeclare wpb_list_child_pages() (previously declared in …/wp-content/themes/pagelines/functions.php:25) in …/wp-content/themes/pagelines/functions.php on line 34

  14. Jeffrey Fry

    How can you also display the date of the child page?

  15. ethann

    Hello great article,
    Can you please help me creating a dropdown of child pages in parent page.I need this functionality for one of my wordpress project and i am totally newbie to wordpress.

  16. Alex

    Am I the only one that cannot get it to work?

    I have the following structure

    About
    — Page 1
    — Page 2
    — Page 3

    When on the “About” or a child page (1, 2 or 3) I would like to have a list with my parent page (About) and all children (1, 2, 3) – Anyone got that working?

    Thanks! :)

    • Kendra

      OMG sorry this is annoying Please ignore my other posts. Didn’t realize you couldn’t paste code into the comments.

      I was NOT able to get it to work with wpb_list_child_pages();

      I WAS able to get it to work with echo do_shortcode( ‘[wpb_childpages]’)

      And to get the title of the parent page, i inserted this above the child page list:
      $current = $post->ID;

      $parent = $post->post_parent;

      $grandparent_get = get_post($parent);

      $grandparent = $grandparent_get->post_parent;

      PHP if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {echo get_the_title($grandparent); }else {echo get_the_title($parent);

  17. Jean Bishop

    I am using this code and it works great. Is it possible to also display categories in menus along with pages?

  18. Michelle

    Hi,

    I want to do the following

    Parent page must be either for sale and or for rent
    Then I want the child for example a province (gauteng) to link to the for sale and the for rent parent.
    How do I do that?

    Michelle

  19. Zakhar

    How can i create shortcode with param, for example, [wpb_childpages id=”1″], where id=”1″ is an id of parent page?

  20. Strand

    Hi,

    Is it possible to limit the links to a specific number such as maximum 12 child pages?

    Thanks

  21. Aander

    Thank you,

    Could you explain, please, how can I organize child pages in drop down list that would be accessible through the parent page? (I don’t want the visitors could see all child pages in a form of a blogroll.)

    I.e., on the parent page I want to create drop down list (listbox) to which child pages would be added in predefined order (say in alphabetical order). After reading Introduction a visitor can proceed by choosing any page from the list at her wish (child pages has no logical connection so in any case she will search the exact page).

    Is ‘my dream’ realizable?

    Thank you again.

  22. Quantum-mecha

    How do I add pagination for Child Page?

    Thanks!

  23. Astrid

    Hi WPBeginner Staff,

    I used the code you guys provided with the “short code option” and it worked (links to child pages). However, I wanted to use the permanent option, and that didn’t work.

    When I added this line of code [ ] the parent page returns a 500 error and no child pages are displayed at atll.

    What am I doing wrong?

    On a side note if I wanted to display and excerpt with its respective image how would I go about it? Thanks!

    • Astrid

      I was able to figure this out.

      I added the code snippet then added this shortcode [wpb_childpages] to the parent page where I wanted the child pages displayed. Awesome!

      Now in order to display excerpts and an image would I use the same queries– just like posts under a category.

      Thanks in advance :)

      • Jade

        Hey I was wondering what you meant by ‘query’? How exactly did you get the excerpts and images to display in the parent page?

        Thanks!

  24. Mehar

    Really useful. Thank you.

  25. Pradeep

    Could someone confirm that the example page used in this tutorial http://optinmonster.com/how-it-works/ uses this feature or not? To me it looks like it they are using tabs, instead of child pages.

    If I’m right, could someone direct me to a resource to get that feature, as I’m desperately looking for a fluid solution like the one they have in that page?

    Many thanks,

    Pradeep

  26. Andrew Roberts

    For the non-shortcode option, you might want to let users know they need to either change
    return $string; to
    echo $string; or
    in their page template echo out the function
    echo page_list_child_pages();

    Thanks to Erik for pointing this out above

    • Meredith Adams

      Thank you!

    • Gaby

      Thank you! I was wondering why the code wouldn’t work…

  27. Howard

    Hi, I can get this to work when I am visiting my domain with a subdirectory (e.g – example.com/home) but when viewing the site without this (example.com) the list of sub pages is not listed. Can anyone point me as to where I am going wrong?

    Thanks in advance – very new to WordPress.

  28. Emily Jennewein

    How do you get this function to display the parent page itself at the top of the list along with the list of child and grandchild pages? On the child pages it does not list the parent page.

  29. Michael

    how can i customized this to just display the child page of a specific page?

  30. Yester

    How do u add a active class on the current ?

  31. Jenny

    Is there now a plugin that can do same ?

    and also can we decide where the box with submenus pages will go in any are of the page not just top of page? Using a plugin

  32. WPBeginner Staff

    Yes it can be used for custom post types. Like this:


    function wpb_list_child_pages() {

    global $post;

    if ( 'movie' == get_post_type() && $post->post_parent )

    $childpages = wp_list_pages( 'post_type=movie&sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    else
    $childpages = wp_list_pages( 'post_type=movie&sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );

    if ( $childpages ) {

    $string = '' . $childpages . '';
    }

    return $string;

    }

    add_shortcode('wpb_childpages', 'wpb_list_child_pages');

  33. Daniela

    This is great! Is there a way to do the same for Custom Post Types that have child posts?

    • Caroline

      Also wondering!

      If anyone knows, please help!

  34. dpc

    Is there a way to modify this in order to print custom post type child posts on a custom post type post page? Thanks, it is very handy!

  35. Sokeara

    It’s very greate for me! Anyways I would like to know how can I display title and thumbnail of child page to parent page.

    • Ashley Bell

      Hi, I have the same query. I really like how the list is styled on the Beginners Blueprint page but I have no idea how to start. Can someone recommend an article to read or give some advice, thanks.

  36. Razvan Zamfir

    Hello!
    Nice snippet, but how do I list only grandchild pages?
    Thank you!

  37. amitabha197

    There is an error in the pages which are not having child pages so I have slightly modified
    if( count($childpages) != 0 ) {
    $string = ” . $childpages . ”;
    }

    instead of
    if( $childpages ){
    $string = ” . $childpages . ”;
    }

  38. WPBeginner Staff

    In order to execute a shortcode in WordPress templates you need to add it like this:

    • Matt Rock

      I couldn’t get the template code “wpb_list_child_pages();” to work, but echo shortcode did the trick. Cheers!

      • Erik

        Just change the “return $string” to “echo $string” (this will print out the pages).

        • fariha

          thanks, it worked

  39. Coen Siebenheller

    I’ve added the code to the functions.php and when i add the [wpb_childpages] shortcode in side my text it works. But when try to add to one of my templates it doesn’t show a thing. Anybody knows what i’m doing wrong?

  40. Elena Rapisardi

    Great explanation!!
    Is there a way to display always the parent pages?

  41. Quin

    This is brilliant, thanks.

    Is there a way to adapt it so the Parent displays the Grandchild, and not the Child?

  42. Dejan

    Great function , thanks !

  43. Richelly Italo

    The great idea, thanks dude.

  44. Steph

    Can you tell us how to stop the list at the first level? I’d like to show all the subpages of that parent, but not the subpages’ children. Thanks.

    • Stefan

      Just add &depth=1

      sort_column=menu_order&title_li=&child_of=’ . $post->post_parent . ‘&echo=0&depth=1

  45. Martin Capodici

    Great. I love this idea of a site plugin and seeing the code you are adding (so you know it is clean and doesn’t do bad stuff) rather than relying on some plugin that could be installing anything.

    For something as simple as listing child pages I agree it should be part of a larger plugin rather than just one plugin otherwise you end up with so many plugins it is hard to keep track.

  46. Rishi Gangoly

    This is great. I had a page on my website called “Services” and was manually linking the sub-pages, but not any more. Awesome. Thanks for this tip. Worked like a charm and did exactly what I was doing manually. :)

  47. Bojan

    This is great! Putting it in a plugin is great feature and great opportunity to extend functionality by adding:
    – child page featured image (as thumbnail)
    – child Title
    – rest of the meta (author, date, comment count)
    – styling in columns maybe
    and so on

    This way, we would have almost complete Portfolio plugin – portfolios made of Pages rather than just made of custom post types.

    Thank you..

  48. yousef mayeli

    hi there
    thanks it was very useful . but if you could show it by video it was very more useable.as tyler moor would do
    thanks
    yousef

  49. Nancy

    This is great! I have a lot of manually added links to child pages, and this is going to make maintaining our site MUCH easier. I’ve just been testing in my development environment, and I’d like to tweak the function a little bit, so that on a child page, when the shortcode is included, all of the children of the same parent are listed EXCEPT for the currently displayed page. I’m really still learning PHP, so while I’ll be trying to figure this one out on my own, I’d welcome any assistance in making that change to the code.

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。