2010-02-23 2 views
9

WordPress에서 형제 페이지 목록 (게시물이 아님)을 만들어 페이지의 사이드 바에 채우려 고합니다. 필자가 작성한 코드는 페이지의 상위 제목을 반환합니다. 내가 알고있는 것처럼Wordpress 페이지의 형제 페이지 목록을 검색하려면 어떻게해야합니까?

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

, 당신은 (wp_list_pages를 통해) 페이지의 형제 자매를 검색 할 (오히려 제목 제외) 페이지의 ID가 필요합니다. 페이지의 상위 ID를 얻으려면 어떻게해야합니까?

대체 접근 방식을 환영합니다. 목표는 반드시 페이지의 형제를 나열하는 것입니다. 반드시 부모의 ID 만 검색하는 것은 아닙니다.

답변

23

$post->post_parent은 부모 ID를 제공하고 있습니다. $post->ID은 현재 페이지 ID를 제공합니다. 그래서, 다음 페이지의 형제 자매를 나열합니다 :

wp_list_pages(array(
    'child_of' => $post->post_parent, 
    'exclude' => $post->ID 
)) 
4
<?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 } ?> 

는 구별 할 .current_page_item 익스 클루 매개 변수, 단지 대상을 사용하지 마십시오.

+0

최고의 솔루션 IMO – benpalmer

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

정답은 형제를 표시하는 것이 아닙니다.

2

이 페이지의 일부 답변은 약간 오래된 정보입니다. 즉 child_of을 사용할 때 exclude이 더 이상 필요하지 않은 것 같습니다. 여기

내 해결책 :이 문제에 대한

// 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>';