
WordPress 父页面
最近我们的一位用户问我们如何显示 WordPress 页面的子页面?通常,在处理包含子页面的网站时,您可能希望在侧边栏小部件或模板中的其他位置的父页面上显示这些子页面。在本文中,我们将向您展示如何在 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 工作原理页面. You can also see this in use on WPBeginner’s 蓝图页.
要查看父页面上子页面列表的示例,请参见OptinMonster的“工作原理” 在页面中以下屏幕截图。 您还可以在WPBeginner的“ 蓝图页上看到此功能。

Before we get start, for those who are not familiar with Child Pages, please check out our guide on the 帖子和页面之间的区别 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)
如果您不喜欢该视频或需要更多说明,请继续阅读。
如果您不喜欢该视频或需要更多说明,请继续阅读。
To create a child page, simply create or edit a page in WordPress like you would normally do. Under the 页面属性 meta box, choose a parent page from the drop down menu.
要创建子页面,只需像通常那样在WordPress中创建或编辑页面即可。 在页面属性元框下,从下拉菜单中选择一个父页面。

注意:如果您没有看到“页面属性”菜单,请单击屏幕右上角的“屏幕选项”按钮。它将显示一个菜单,您需要在其中确保选中页面属性。
注意:如果您没有看到“页面属性”菜单,请单击屏幕右上角的“屏幕选项”按钮。它将显示一个菜单,您需要在其中确保选择页面属性。
在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 特定于站点的插件, or in your theme’s 函数.php file:
要在父页面下列出子页面,您需要在具体的于网站插件或主题的函数.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 短代码, so you can easily display child pages without modifying your page templates.
上面的代码首先检查页面是否具有父级,或者页面本身是父级。 如果它是父页面,则显示与其关联的子页面。 如果它是子页面,则显示其父页面的所有其他子页面。 最后,如果这只是一个没有子页面或父页面的页面,那么代码将什么都不做。 在代码的最后一行,我们添加了一个短代码 ,因此您可以轻松显示子页面而无需修改页面模板。
要显示子页面,只需在侧边栏中的页面或文本小部件中添加以下短代码:
要显示子页面,只需将以下短代码添加到侧边栏中的页面或文本小部件中:
[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 如何在 WordPress 侧边栏小部件中使用短代码.
在某些情况下,您的主题可能尚未准备好在文本窗口小部件中执行短代码。 如果不起作用,请参阅本教程, 了解如何在 WordPress 侧边栏小部件中使用短代码 。
动态显示子页面而没有任何简码 (Dynamically Display Child Pages Without Any 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 创建自定义页面模板 in your theme.
更好的方法是在主题中编辑页面模板文件,以便它可以自动显示子页面。 为此,您需要编辑page.php
模板或在主题中创建自定义页面模板 。
在页面模板文件中,您需要在要显示子页面的位置添加这行代码。
在页面模板文件中,您需要在希望子页面出现的位置添加这行代码。
<?php wpb_list_child_pages(); ?>
就这样。您的主题现在将自动检测子页面并显示它们。
就这样。现在,您的主题将自动检测并显示子页面。
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 管理栏视图.
如果您使用的父页面具有很多子页面,而这些子页面具有自己的子页面,则WordPress管理员视图可能会造成混淆。 为了更好地组织父级和页面,请尝试使用管理列视图 。
我们希望本文能帮助您列出 WordPress 中的子页面。如果您有任何问题或反馈,请在下面发表评论,让我们知道。
我们希望本文能帮助您列出 WordPress 中的子页面。如果您有任何问题或反馈,请在下面发表评论并告诉我们。
Source: 托马斯·格里芬
资料来源: 托马斯·格里芬
WordPress 父页面