如何在WordPress的所有页面上显示一位父级的子页面
我试图在我的 WordPress 网站上的所有页面的页脚中显示地毯的子页面。
我使用的页脚中的代码是
<?php
global $wp_query;
$post = $wp_query->post;
$ancestors = get_post_ancestors($post);
if( empty($post->post_parent) ) {
$parent = $post->ID;
} else {
$parent = end($ancestors);
}
if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) { ?>
<ul class="footerNav clearfix">
<?php wp_list_pages("title_li=&child_of=$parent&depth=1" ); ?>
</ul><!-- #secondary-nav -->
<?php } ?>
,但是,当您位于相关类别时,这只显示页脚中的子页面,我希望在所有页面上看到此导航。
谢谢,
周六
这里是带有页脚的页面的链接,我希望它在所有页面上如何
I am trying to show the child pages of rugs in the footer in all pages on my wordpress site.
the code in the footer i am using is
<?php
global $wp_query;
$post = $wp_query->post;
$ancestors = get_post_ancestors($post);
if( empty($post->post_parent) ) {
$parent = $post->ID;
} else {
$parent = end($ancestors);
}
if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) { ?>
<ul class="footerNav clearfix">
<?php wp_list_pages("title_li=&child_of=$parent&depth=1" ); ?>
</ul><!-- #secondary-nav -->
<?php } ?>
however this only shows the child pages in the footer when you are in the relevant catagory, I would like to see this navigation on all pages.
Thanks,
Sat
here is the link to a page with the footer how i'd like it on all pages
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用
变量 $post 分配访问者当前正在查看的帖子。
如果您希望所有页面上都有 rugs 的所有子项菜单,则只需要代码的底部部分。另外,child_of参数不需要通过变量赋值,可以输入rugs的静态页面ID,即173:
这三行将足以为您生成菜单。它周围的 if 语句检查特定父页面是否有子页面,并且仅在这种情况下生成无序列表。既然您知道它存在于这种情况下,我将省略它并保存这两行。为了完整起见,我添加了 sort_column 参数 - 这将为您提供所有地毯按字母顺序排序的菜单。
if 语句上方的代码仅适用于根据访问者当前所在页面而变化的动态菜单。如果您想实现这样的菜单,我仍然会以不同的方式进行操作,并认为上面的内容过于臃肿。只需插入
到您的 header.php 文件中,您就可以在其他地方生成动态菜单 如上所述
,对于您的特定情况,第一个三行代码块就足够了。
进一步参考:WP Codex:wp_list_pages
By using
the variable $post is assigned the post the visitor is currently viewing.
If you want a menu of all childs of rugs on all pages, you only need the bottom part of your code. Also, the child_of parameter needs not be assigned via variable, you can input the static page ID of rugs, i.e. 173:
These three lines will suffice in generating the menu for you. The if statement you have around it checks whether a the specific parent page has children and only generates an unordered list if that is the case. Since you know it exists in this case, I'd leave it out and save those two lines. I have included the sort_column parameter for sake of completeness - this would give you an alphabetically sorted menu of all rugs.
The code above the if statement is only needed for dynamic menus that change depending on the page the visitor is currently on. Should you ever want to implement such a menu, I'd still go about it differently and think the above is bloated. Simply inserting
into your header.php file will let you generate dynamic menus elsewhere with
As said above, for your particular case, the fist code block three-liner is sufficient.
Further reference: WP Codex: wp_list_pages