You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress父页面与子页面侧边栏显示子页面链接

解决WordPress侧边栏子页面导航显示问题

首先,咱们来拆解你的问题:你希望侧边栏在父页面和它的所有子页面中,都能显示该父页面下的子页面导航,同时展示两篇最新文章。当前代码的问题在于,在子页面中你用了$post->ID作为post_parent查询参数,这会导致只查找当前子页面的子页面(而不是父页面的子页面),所以子页面上看不到需要的导航。

修复后的完整侧边栏代码

下面是整合了正确子页面导航+最新文章的代码,直接替换你原来的侧边栏代码即可:

<?php
// 第一步:获取当前页面的顶级父页面ID(确保在父/子页面都能拿到正确的父页面)
$current_page_id = get_the_ID();
$top_parent_id = $current_page_id;

// 循环找到最顶层的父页面(如果当前是子页面,会向上追溯到根父页面)
while ($parent_id = wp_get_post_parent_id($top_parent_id)) {
    $top_parent_id = $parent_id;
}

// 第二步:查询顶级父页面下的所有子页面
$child_pages_args = array(
    'post_type'      => 'page',
    'posts_per_page' => 10,
    'post_parent'    => $top_parent_id,
    'order'          => 'ASC',
    'orderby'        => 'menu_order',
    'post_status'    => 'publish' // 确保只显示已发布页面
);
$child_pages_query = new WP_Query($child_pages_args);

// 第三步:显示子页面导航
if ($child_pages_query->have_posts()) : ?>
<section class="links border shadow">
    <h3><?php echo get_the_title($top_parent_id); ?> 子页面</h3>
    <ul>
        <!-- 可选:显示父页面链接 -->
        <li <?php echo (get_the_ID() == $top_parent_id) ? 'class="current-menu-item"' : ''; ?>>
            <a href="<?php echo get_permalink($top_parent_id); ?>"><?php echo get_the_title($top_parent_id); ?></a>
        </li>
        <?php while ($child_pages_query->have_posts()) : $child_pages_query->the_post(); ?>
        <li <?php echo (get_the_ID() == $current_page_id) ? 'class="current-menu-item"' : ''; ?>>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            <!-- 如果需要显示子页面的子页面(孙页面),可以在这里添加嵌套查询 -->
        </li>
        <?php endwhile; ?>
    </ul>
</section>
<?php 
endif;
wp_reset_postdata(); // 重置查询,避免影响后续内容

// 第四步:显示两篇最新文章
$latest_posts_args = array(
    'post_type'      => 'post',
    'posts_per_page' => 2,
    'order'          => 'DESC',
    'orderby'        => 'date',
    'post_status'    => 'publish'
);
$latest_posts_query = new WP_Query($latest_posts_args);

if ($latest_posts_query->have_posts()) : ?>
<section class="latest-posts border shadow">
    <h3>最新文章</h3>
    <ul>
        <?php while ($latest_posts_query->have_posts()) : $latest_posts_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            <p class="post-date"><?php echo get_the_date(); ?></p>
        </li>
        <?php endwhile; ?>
    </ul>
</section>
<?php 
endif;
wp_reset_postdata();
?>

关键修复点说明

  • 正确获取父页面ID:
    用wp_get_post_parent_id()循环追溯,确保无论当前是父页面还是多级子页面,都能拿到最顶层的父页面ID。如果你的需求是显示直接父页面的子页面,只需要把循环部分改成$display_parent_id = wp_get_post_parent_id($current_page_id) ?: $current_page_id;即可。
  • 修复查询参数:
    把原来的post_parent => $post->ID替换为post_parent => $top_parent_id,这样就能在子页面中正确查询到父页面的所有子页面。
  • 添加当前页面高亮:
    通过对比get_the_ID()和当前页面ID,给活跃页面添加current-menu-item类,方便样式区分。
  • 整合最新文章模块:
    新增了最新文章的查询,设置posts_per_page => 2,按发布时间倒序排列,满足你的需求。

关于你参考的第二段代码

那段代码的逻辑其实是对的,但它只处理了直接父页面的情况,并且没有处理多级子页面的场景。咱们上面的代码优化了这一点,同时添加了更多的健壮性(比如post_status参数)和扩展性。

内容的提问来源于stack exchange,提问作者MattHamer5

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:37:15