0

I need help listing all pages when on a third level (grandchild) Page.

E.g I have Page 1(grandparent) Page 2(Parent) Page 3(Child)

I need to show all these pages listed the same on all three pages such as:

Page1 Page2 Page3

I have successfully shown the right list of pages on page 1 and 2. (see below)

Can anyone please help

function widget($args, $instance) { 

    global $post;
    extract( $args );

    if($post->post_parent == 0) {
        $title = '<a href="'.get_permalink($post->ID).'" title="'.$post->post_title.'">'.$post->post_title.'</a>';
        $id_to_query = $post->ID;
    }
    elseif($post->ancestors) {
        $page = get_page($post->ancestors[0]);
        $title = '<a href="'.get_permalink($page->ID).'" title="'.$page->post_title.'">'.$page->post_title.'</a>';          
        $id_to_query = $post->ancestors[0];
    } else {
        $page = get_page($post->post_parent);
        $title = '<a href="'.get_permalink($page->ID).'" title="'.$page->post_title.'">'.$page->post_title.'</a>';
        $id_to_query = $page->ID;
    }       

    $children = get_pages('post_type='.get_post_type().'&child_of='.$id_to_query);
    if(empty($children) || is_page( array(17,125) ) ) return; // excludes contact us etc...     

    wp_reset_query();

    $widget_title = $title;


    echo $before_widget;
    echo $before_title . $widget_title . $after_title; ?>
    <ul>
        <?php wp_list_pages('title_li=&post_type='.get_post_type().'&child_of='.$id_to_query); ?>
    </ul>           
    <?php
    echo $after_widget;
    wp_reset_postdata();
4

1 に答える 1

0

$topid = get_top_ancestor($postid);引数を使用して最上位の祖先IDを取得してみて'child_of' => $topiddepth

完全な例:

   <?php 

    $postid = get_the_ID();
    $topid = get_top_ancestor($postid); 

    $args = array(
            'depth'        => 2,
            'show_date'    => '',
            'date_format'  => get_option('date_format'),
            'child_of'     => $topid,
            'exclude'      => '',
            'include'      => '',
            'echo'         => 1,
            'authors'      => '',
            'sort_order'   => 'ASC',
            'link_before'  => '',
            'link_after'   => '',
            'walker'       => '',
            'post_type'    => 'page',
            'post_status'  => 'publish' 
    ); ?>

    <ul><?php wp_list_pages( $args ); ?></ul>

[未テスト]で作業するための何か:

function widget($args, $instance) { 

global $post; 

$postid = get_the_ID();
$topid = get_top_ancestor($postid); 

$args = array(
        'depth'        => 2,
        'show_date'    => '',
        'date_format'  => get_option('date_format'),
        'child_of'     => $topid,
        'exclude'      => '',
        'include'      => '',
        'echo'         => 1,
        'authors'      => '',
        'sort_order'   => 'ASC',
        'link_before'  => '',
        'link_after'   => '',
        'walker'       => '',
        'post_type'    => 'page',
        'post_status'  => 'publish' 
);

// if(empty($children) || is_page( array(17,125) ) ) return; // excludes contact us etc...     

// wp_reset_query();

$widget_title = $title;


echo $before_widget;
echo $before_title . $widget_title . $after_title; ?>
<ul>
    <?php wp_list_pages( $args ); ?>
</ul>           
<?php
echo $after_widget;
wp_reset_postdata();

 }
于 2012-08-21T12:44:31.080 に答える