简体   繁体   English

如何在所有页面上显示一位家长的子页面

[英]how to show child pages of one parent on all pages wordpress

I am trying to show the child pages of rugs in the footer in all pages on my wordpress site. 我试图在我的wordpress网站上的所有页面的页脚中显示地毯的子页面。

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/ http://satbulsara.com/luke-irwin/rugs/new-in/fishy/

By using 通过使用

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

the variable $post is assigned the post the visitor is currently viewing. 变量$ post被分配给访问者当前正在查看的帖子。

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 , ie 173 : 另外, 不需要通过变量分配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 -->

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. 您周围的if语句检查特定的父页面是否具有子页面,并且仅在这种情况下才生成无序列表。 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. 为了完整起见,我包括了sort_column参数-这将为您提供所有地毯的按字母顺序排序的菜单。

The code above the if statement is only needed for dynamic menus that change depending on the page the visitor is currently on. if语句上方的代码仅适用于根据访问者当前所在页面而变化的动态菜单。 Should you ever want to implement such a menu, I'd still go about it differently and think the above is bloated. 如果您想实现这样的菜单,我还是会另辟about径,并认为上面的内容很肿。 Simply inserting 只需插入

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

into your header.php file will let you generate dynamic menus elsewhere with 进入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 } ?>

As said above, for your particular case, the fist code block three-liner is sufficient. 如上所述,对于您的特定情况,第一代码块三层就足够了。

Further reference: WP Codex: wp_list_pages 进一步的参考: WP Codex:wp_list_pages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM