在WordPress中,要獲取同級頁面的標題和鏈接,可以通過以下步驟實現:

獲取當前頁面的父頁面ID:首先,需要確定當前頁面所屬的父頁面ID。這可以通過get_post_ancestors()函數來實現,該函數能夠返回指定頁面的所有父頁面ID 。

遍歷同級頁面:一旦獲得了父頁面ID,就可以通過查詢該父頁面下的子頁面來獲取同級頁面。WordPress提供了多種方式來查詢頁面,例如使用get_pages()函數或者直接通過數據庫查詢。

獲取同級頁面的標題和鏈接:對于每個同級頁面,可以使用the_title()和the_permalink()函數分別獲取頁面的標題和鏈接。the_title()函數用于顯示或檢索博客所有區域的頁面標題 ,而the_permalink()函數則用于輸出頁面的永久鏈接 。

處理特殊情況:如果遇到復雜的頁面結構或者需要考慮SEO優化等因素,可能還需要進一步處理,比如動態生成自定義的頁面標題和鏈接,以適應不同的顯示需求。

<?php
//Get the parent page ID of the current page
$parent_id = wp_get_post_parent_id(get_the_ID());
//Get parameters for peer pages with the same parent level
$args = array(
    'post_type' => 'page',
    'post_parent' => $parent_id,
    'posts_per_page' => -1,
    'orderby' => 'menu_order',
    'order' => 'ASC',
    'post__not_in' => array(get_the_ID()) //Exclude current page
);
//Query peer pages with the same parent level
$sibling_pages = new WP_Query($args);
//Output titles and links to pages of the same level with the same parent level
if ($sibling_pages->have_posts()) {
    echo '<ol>';
    while ($sibling_pages->have_posts()) {
        $sibling_pages->the_post();
        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    }
    echo '</ol>';
}
//Reset Query
wp_reset_postdata();
?>

獲取WordPress子頁面的同級頁面標題和鏈接涉及到識別當前頁面的父頁面ID,然后遍歷該父頁面下的所有子頁面,并為每個子頁面獲取其標題和鏈接。這一過程可以通過結合使用WordPress提供的各種函數和API來實現。