wordpress 父页面_如何在WordPress中显示父页面的子页面列表

wordpress 父页面

Recently one of our users asked us how to display child pages of a WordPress Page? Often when working on a site that has pages with child pages, you may want to show those child pages on the parent page in a sidebar widget or another location in your template. In this article, we will show you how to display a list of child pages for a parent page in WordPress.

最近,我们的一位用户问我们如何显示WordPress页面的子页面? 通常,在具有包含子页面的页面的网站上工作时,您可能希望在侧边栏小部件或模板中的其他位置的父页面上显示这些子页面。 在本文中,我们将向您展示如何在WordPress中显示父页面的子页面列表。

To see an example of a list of child pages on parent page, see the screenshot below that we have from OptinMonster’s How it Works page. You can also see this in use on WPBeginner’s Blueprint page.

要查看父页面上子页面列表的示例,请参见OptinMonster的“工作原理” 页面中的以下屏幕截图。 您还可以在WPBeginner的“ 蓝图”页面上看到此功能。

A parent page with a list of child pages

Before we get start, for those who are not familiar with Child Pages, please check out our guide on the difference between Posts and Pages in WordPress. One of the important feature of pages is that they can be hierarchical. This means that a page can become a parent page and has child pages (i.e sub-pages) under it. This allows you to group different pages together under one parent page. For example, if you have a Product Page on a website, then you can add pages such as Features, Pricing, and Support as child pages. Each child page can have its own child pages as well.

在开始之前,对于那些不熟悉子页面的用户,请查看有关WordPress中帖子和页面之间区别的指南。 页面的重要特征之一是它们可以是分层的。 这意味着页面可以成为父页面并在其下具有子页面(即子页面)。 这使您可以将一个父页面下的不同页面组合在一起。 例如,如果您在网站上有一个“产品页面”,则可以将诸如“功能”,“定价”和“支持”之类的页面添加为子页面。 每个子页面也可以有自己的子页面。

影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.

如果您不喜欢该视频或需要更多说明,请继续阅读。

To create a child page, simply create or edit a page in WordPress like you would normally do. Under the Page Attributes meta box, choose a parent page from the drop down menu.

要创建子页面,只需像通常那样在WordPress中创建或编辑页面即可。 在页面属性元框下,从下拉菜单中选择一个父页面。

Creating a child page by assigning it a Parent page in WordPress

Note: If you do not see the Page Attributes menu, then please click on the Screen Options button on the top right hand corner of your screen. It will display a menu where you need to make sure that Page Attributes is checked.

注意:如果看不到“页面属性”菜单,请单击屏幕右上角的“屏幕选项”按钮。 它将显示一个菜单,您需要在其中确保选中页面属性。

在WordPress的父页面上显示子页面 (Displaying Child Pages on the Parent Page in WordPress)

To list child pages under a parent page, you need to add the following code in a site-specific plugin, or in your theme’s functions.php file:

要在父页面下列出子页面,您需要在特定站点的插件或主题的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>' . $childpages . '</ul>';
}

return $string;

}

add_shortcode('wpb_childpages', 'wpb_list_child_pages');

The code above first checks to see if a page has a parent or the page itself is a parent. If it is a parent page, then it displays the child pages associated with it. If it is a child page, then it displays all other child pages of its parent page. Lastly, if this is just a page with no child or parent page, then the code will simply do nothing. In the last line of the code, we have added a shortcode, so you can easily display child pages without modifying your page templates.

上面的代码首先检查页面是否具有父级,或者页面本身是父级。 如果它是父页面,则显示与其关联的子页面。 如果它是子页面,则显示其父页面的所有其他子页面。 最后,如果这只是一个没有子页面或父页面的页面,那么代码将什么都不做。 在代码的最后一行,我们添加了一个shortcode ,因此您可以轻松显示子页面而无需修改页面模板。

To display child pages simply add the following shortcode in a page or text widget in the sidebar:

要显示子页面,只需在边栏中的页面或文本小部件中添加以下短代码:

[wpb_childpages]

[wpb_childpages]

In some cases, your theme may not be ready to execute shortcodes in a text widget. If it is not working, then see this tutorial on how to use shortcodes in WordPress sidebar widgets.

在某些情况下,您的主题可能尚未准备好在文本窗口小部件中执行短代码。 如果不起作用,请参阅本教程, 了解如何在WordPress侧边栏小部件中使用短代码

动态显示子页面而没有任何简码 (Dynamically Display Child Pages Without Any Shortcode)

Using shortcode is convenient, but the problem with using shortcodes is that you will have to add shortcode in all pages that have parent or child pages. You may end up having shortcodes in lots of pages, and sometimes you may even forget to add the shortcode.

使用简码很方便,但是使用简码的问题是您必须在具有父页面或子页面的所有页面中添加简码。 您可能最终在许多页面中都使用了简码,有时甚至可能忘记添加简码。

A better approach would be to edit the page template file in your theme, so that it can automatically display child pages. To do that you need to edit the main page.php template or create a custom page template in your theme.

更好的方法是在主题中编辑页面模板文件,以便它可以自动显示子页面。 为此,您需要编辑page.php模板或在主题中创建自定义页面模板

In your page template file, you need to add this line of code where you want to display child pages.

在页面模板文件中,您需要在要显示子页面的位置添加此代码行。


<?php wpb_list_child_pages(); ?>

That’s all. Your theme will now automatically detect child pages and display them.

就这样。 现在,您的主题将自动检测并显示子页面。

If you are using parent pages with lots of child pages that have their own child pages, then the WordPress admin view can get confusing. For a better way to organize parent and pages try using admin column view.

如果您使用的父页面具有很多子页面,而这些子页面具有自己的子页面,则WordPress管理员视图可能会造成混淆。 为了更好地组织父级和页面,请尝试使用管理列视图

We hope this article helped you list child pages in WordPress. Let us know if you have any questions or feedback by leaving a comment below.

我们希望本文能帮助您列出WordPress中的子页面。 请在下面留下评论,让我们知道您是否有任何疑问或反馈。

Source: Thomas Griffin

资料来源: 托马斯·格里芬(Thomas Griffin)

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/

wordpress 父页面

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值