如何在WordPress的所有页面上显示一位父级的子页面

发布于 2024-11-08 01:32:31 字数 911 浏览 0 评论 0原文

我试图在我的 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 } ?>

,但是,当您位于相关类别时,这只显示页脚中的子页面,我希望在所有页面上看到此导航。

谢谢,

周六

这里是带有页脚的页面的链接,我希望它在所有页面上如何

http://satbulsara.com/luke-irwin/rugs/new-in/fishy/

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

http://satbulsara.com/luke-irwin/rugs/new-in/fishy/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星光不落少年眉 2024-11-15 01:32:31

通过使用

$post = $wp_query->post;
$ancestors = get_post_ancestors($post);

变量 $post 分配访问者当前正在查看的帖子。

如果您希望所有页面上都有 rugs 的所有子项菜单,则只需要代码的底部部分。另外,child_of参数不需要通过变量赋值,可以输入rugs的静态页面ID,即173

<ul class="footerNav clearfix">
    <?php wp_list_pages("title_li=&child_of=173&depth=1&sort_column=post_name" ); ?>
</ul><!-- #secondary-nav -->

这三行将足以为您生成菜单。它周围的 if 语句检查特定父页面是否有子页面,并且仅在这种情况下生成无序列表。既然您知道它存在于这种情况下,我将省略它并保存这两行。为了完整起见,我添加了 sort_column 参数 - 这将为您提供所有地毯按字母顺序排序的菜单。

if 语句上方的代码仅适用于根据访问者当前所在页面而变化的动态菜单。如果您想实现这样的菜单,我仍然会以不同的方式进行操作,并认为上面的内容过于臃肿。只需插入

<?php $ancestors = get_post_ancestors($post); ?>

到您的 header.php 文件中,您就可以在其他地方生成动态菜单 如上所述

<?php if (is_page(173) || in_array(173,$ancestors)) { ?>
    <ul class="subnav">
        <?php wp_list_pages('title_li=&child_of=173&depth=1'); ?>
    </ul>
<?php } ?>

,对于您的特定情况,第一个三行代码块就足够了。

进一步参考:WP Codex:wp_list_pages

By using

$post = $wp_query->post;
$ancestors = get_post_ancestors($post);

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:

<ul class="footerNav clearfix">
    <?php wp_list_pages("title_li=&child_of=173&depth=1&sort_column=post_name" ); ?>
</ul><!-- #secondary-nav -->

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

<?php $ancestors = get_post_ancestors($post); ?>

into your header.php file will let you generate dynamic menus elsewhere with

<?php if (is_page(173) || in_array(173,$ancestors)) { ?>
    <ul class="subnav">
        <?php wp_list_pages('title_li=&child_of=173&depth=1'); ?>
    </ul>
<?php } ?>

As said above, for your particular case, the fist code block three-liner is sufficient.

Further reference: WP Codex: wp_list_pages

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文