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. Philip

    Hello this works, except that the parent is also displaying, how do i show just the child pages, thanks

    • WPBeginner Support

      The simplest method for what it sounds like you’re wanting would be to not have content on the parent page and only have your content in the child pages.

      管理者

  3. Sachit Shori

    Thank you so much. You saved me.

    • WPBeginner Support

      You’re welcome :)

      管理者

  4. Emmanuel Husseni

    Hello Wpbeginner,

    Please how can i sort all the child page alphabetically on the parent page. I’ve follow all step but the sub pages are showing randomly on the parent page.

    Waiting for response. ….Thanks

    • WPBeginner Support

      You would change the two instances of menu_order in our code to be: post_title

      管理者

  5. Iyke O.

    How do I make the list collapsible on sidebar.

  6. Stacie

    Easy to follow and did the job. Thanks so much for sharing!

    • WPBeginner Support

      You’re welcome, glad our content could be helpful :)

      管理者

  7. Aaro

    Can I assign a css class to this function? So that when I make css changes to ul elements it wouldn’t affect other ul’s on the site.
    Or any other simple solution for this?

    • WPBeginner Support

      You could add your CSS class in the ul section of the function :)

      管理者

  8. Keshav Murthy

    Hi, WPB Team,

    Thank you so much for this Snippet and the tutorial.

    It saved my ton of time and helped me too.

    With Warm Regards,
    Keshav Murthy

    • WPBeginner Support

      Glad our tutorial could help :)

      管理者

  9. Gary Granai

    I installed the plugin code snippets in wordpress 4.9.8

    I copied the code on https://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/#respond and added it to a new snippet in code snippets.

    I made a page and then a page which was given the attribute of having the first page as the parent page.

    The child parent relationship is shown in the list of pages in the attributes drop down.

    When I open the parent page I see nothing that shows a child page.

    I then tried using the functions.php page.

    I added the code copied from https://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/#respond to the bottom of the code on the functions page.

    When I open the parent page, I it does not display. What displays is an error message saying there is unexpected code.

    I restored the functions.php page to its original statuc.

    What changes must I make to what I have tried to do.

    • WPBeginner Support

      Hi Gary,

      Make sure that you publish the child page before testing the code. Also carefully copy the code again to make sure that you are not copying the numbers or any unexpected characters.

      管理者

  10. Itika

    I have a question. I have added sub pages in parent page but when I am opening the site in mobile and clicking on parent page it opens the empty parent page. To view the drop down sub pages one have to hold the button of parent page. How can I fix it. I don’t want that empty page to open. I want that if we touch on parent page it opens the drop down menu.
    Please suggest how to do it.

返信を残す

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