2014-01-12 31 views
0

我直接從代碼中未經編輯的&此代碼,它基本上列出了當前頁面的子代。但是,我想刪除父/祖的列表。非常感激任何的幫助。WordPress - 從此刪除祖先列表

謝謝!

<?php 
//if the post has a parent 
if($post->post_parent){ 
    //collect ancestor pages 
    $relations = get_post_ancestors($post->ID); 
    //get child pages 
    $result = $wpdb->get_results("SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type='page'"); 
    if ($result){ 
    foreach($result as $pageID){ 
     array_push($relations, $pageID->ID); 
    } 
    } 
    //add current post to pages 
    array_push($relations, $post->ID); 
    //get comma delimited list of children and parents and self 
    $relations_string = implode(",",$relations); 
    //use include to list only the collected pages. 
    $sidelinks = wp_list_pages("title_li=&echo=0&include=".$relations_string); 
}else{ 
    // display only main level and children 
    $sidelinks = wp_list_pages("title_li=&echo=0&depth=1&child_of=".$post->ID); 
} 

if ($sidelinks) { ?> 
    <h2><?php the_title(); ?></h2> 
    <ul> 
    <?php //links in <li> tags 
    echo $sidelinks; ?> 
    </ul>   
<?php } ?> 

回答

1

我認爲沒有必要使問題複雜化。如果你想獲得當前職位的孩子,只需檢查它是否有孩子,如果是,顯示他們。你當然需要適應你的需求。但只是展示如何獲得它們。你想做什麼

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

這個鏈接是有用的: http://codex.wordpress.org/Function_Reference/wp_list_pages

+0

這是完美的,工程完全按照我想要的。謝謝你Nimle的幫助:) – user2325396

+0

嗨Nimle,我如何列出只有一層兒童而不是多層? Thx – user2325396

+0

您可以添加其他參數進行過濾:深度可能是您想要的。例如,depth = 1 wp_list_pages(「depth = 1&title_li =&child_of =」。$ post-> ID。「&echo = 0」); 或者如果你想看到但不打印,你可以檢查孩子的父母id,看它是否等於當前的帖子ID。如果不是,那就意味着它不是一個直接的孩子。 – Selay