【问题标题】:How to get all subpages from the current mainpage如何从当前主页获取所有子页面
【发布时间】:2021-05-21 15:14:02
【问题描述】:

例如,我有这个菜单,并添加到页面 About 和 Vacancy 子页面,并将它们添加到页面设置中

Home
About
-- History
-- Photo's
Vacancies
-- Front-end developer
-- Full-stack developer
Contact

当我在子页面上时,如何获取当前主页?例如我在主页关于,而不是我想要所有子页面的列表(在这个例子中是历史和照片)

页面空缺也是如此,当我在主页上时,我想获取所有子页面的列表。

我在下面尝试过,但后来我得到了 cpt 'page' 的所有页面。不是我需要的孩子。有人可以帮忙吗? :)

    <?php
    global $post;

    $args = array(
        'child_of'          => $post->ID,
        'post_type'         => 'page',
        'posts_per_page'    => -1,
        'order'             => 'ASC',
        'depth'             => 1,
        'orderby'           => 'date',
    );

    $parent = new WP_Query($args); ?>

    <?php if ($parent->have_posts()) : ?>

        <div class="submenu-pages">

            <ul>
                <?php while ($parent->have_posts()) : $parent->the_post(); ?>
                    <li>
                        <a class="submenu-pages__link" href="<?php the_permalink(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </li>
                <?php endwhile; ?>
            </ul>
        </div>

    <?php endif; ?>

    <?php wp_reset_query(); ?>

【问题讨论】:

  • 您在问两个不同的问题 1) 我在子页面上时如何获取当前主页? 2)当我在主页上时,我想获取所有子页面的列表。

标签: wordpress


【解决方案1】:

child_of(和depth)与wp_list_pages()一起使用;不作为 WP_Query 类的参数的一部分。

因此,您需要做的就是让您的代码正常工作(new WP_Query() 部分),使用 post_parent 检索属于指定父级的子级的帖子:

$args = array(
    'post_parent'    => $post->ID, // use post_parent and not child_of
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'order'          => 'ASC',
    'orderby'        => 'date',
);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-06-17
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
相关资源
最近更新 更多