在WordPress中,要獲取同級(jí)頁面的標(biāo)題和鏈接,可以通過以下步驟實(shí)現(xiàn):

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

遍歷同級(jí)頁面:一旦獲得了父頁面ID,就可以通過查詢?cè)摳疙撁嫦碌淖禹撁鎭慝@取同級(jí)頁面。WordPress提供了多種方式來查詢頁面,例如使用get_pages()函數(shù)或者直接通過數(shù)據(jù)庫查詢。

獲取同級(jí)頁面的標(biāo)題和鏈接:對(duì)于每個(gè)同級(jí)頁面,可以使用the_title()和the_permalink()函數(shù)分別獲取頁面的標(biāo)題和鏈接。the_title()函數(shù)用于顯示或檢索博客所有區(qū)域的頁面標(biāo)題 ,而the_permalink()函數(shù)則用于輸出頁面的永久鏈接 。

處理特殊情況:如果遇到復(fù)雜的頁面結(jié)構(gòu)或者需要考慮SEO優(yōu)化等因素,可能還需要進(jìn)一步處理,比如動(dòng)態(tài)生成自定義的頁面標(biāo)題和鏈接,以適應(yīng)不同的顯示需求。

<?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子頁面的同級(jí)頁面標(biāo)題和鏈接涉及到識(shí)別當(dāng)前頁面的父頁面ID,然后遍歷該父頁面下的所有子頁面,并為每個(gè)子頁面獲取其標(biāo)題和鏈接。這一過程可以通過結(jié)合使用WordPress提供的各種函數(shù)和API來實(shí)現(xiàn)。