在WordPress中通过PHP显示兄弟页面

时间:2018-07-30 16:50:18

标签: php wordpress

我不了解PHP,因此已经难住了。

但是我想要的是:

对于与当前页面具有相同父级的每个页面(所以同级页面)显示:

<?php the_permalink(); ?> <?php the_title(); ?>

但只能循环12次。

如果有人可以帮助我,我将非常感激。

1 个答案:

答案 0 :(得分:0)

获取父ID,然后使用该ID获取子帖子。将它们限制为12个帖子,并排除当前ID。然后遍历结果。

<?php
$parentId = wp_get_post_parent_id( get_the_ID() );

$children = get_posts( [
    'posts_per_page' => 12,
    'post_parent'    => $parentId,
    'post__not_in'   => [ get_the_ID() ] // exclude current page
] );
?>
<ul class="section-nav">
    <?php foreach( $children as $child ): ?>
        <li class="section-nav-item">
            <a href="<?= get_the_permalink( $child->ID ); ?>">
                <?= get_the_title( $child->ID ); ?>
            </a>
        </li>
    <?php endforeach; ?>
</ul>