programing

Wordpress 페이지의 형제 페이지 목록을 검색하려면 어떻게 해야 합니까?

mytipbox 2023. 3. 4. 13:57
반응형

Wordpress 페이지의 형제 페이지 목록을 검색하려면 어떻게 해야 합니까?

페이지의 사이드바를 채우기 위해 WordPress에서 형제 페이지 목록(게시 아님)을 작성하려고 합니다.내가 작성한 코드가 페이지 상위 제목을 반환합니다.

<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title; ?>

페이지의 형제(wp_list_pages)를 검색하려면 (제목이 아닌) 페이지 ID가 필요한 것으로 알고 있습니다.페이지 부모 아이디는 어떻게 얻을 수 있나요?

다른 접근방식은 환영입니다.목표는 페이지의 형제 목록을 표시하는 것이지 반드시 상위 ID를 검색하는 것은 아닙니다.

$post->post_parent부모 아이디를 알려주고 있습니다.$post->ID현재 페이지 ID가 표시됩니다.따라서 페이지의 형제 목록은 다음과 같습니다.

wp_list_pages(array(
    'child_of' => $post->post_parent,
    'exclude' => $post->ID
))
wp_list_pages(array(
    'child_of' => $post->post_parent,
    'exclude' => $post->ID,
    'depth' => 1
));

정답은 다른 두 정답 모두 형제자매만 표시되는 것은 아니기 때문에 정답입니다.

이 페이지의 답변 중 일부는 약간 오래된 정보를 가지고 있습니다.즉,exclude사용할 때 더 이상 필요하지 않은 것 같다child_of.

제 해결책은 다음과 같습니다.

// if this is a child page of another page,
// get the parent so we can show only the siblings
if ($post->post_parent) $parent = $post->post_parent;
// otherwise use the current post ID, which will show child pages instead
else $parent = $post->ID;

// wp_list_pages only outputs <li> elements, don't for get to add a <ul>
echo '<ul class="page-button-nav">';

wp_list_pages(array(
    'child_of'=>$parent,
    'sort_column'=>'menu_order', // sort by menu order to enable custom sorting
    'title_li'=> '', // get rid of the annoying top level "Pages" title element
));

echo '</ul>';
<?php if($post->post_parent): ?>
<?php $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); ?>
<?php else: ?>
<?php $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0'); ?>
<?php endif; ?>
<?php if ($children) { ?>
<ul class="subpage-list">
<?php echo $children; ?>
</ul>
<?php } ?>

exclude 파라미터는 사용하지 말고 그 .current_page_item을 타겟으로 하여 구별합니다.

언급URL : https://stackoverflow.com/questions/2322271/how-can-i-retrieve-a-list-of-a-wordpress-pages-sibling-pages

반응형