0

在第三级(孙子)页面上时,我需要列出所有页面的帮助。

例如,我有第 1 页(祖父母) 第 2 页(父母) 第 3 页(孩子)

我需要在所有三个页面上显示相同列出的所有这些页面,例如:

第 1 页 第 2 页 第 3 页

我已经成功地在第 1 页和第 2 页显示了正确的页面列表。(见下文)

谁能帮忙

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

尝试获取最高祖先 id,$topid = get_top_ancestor($postid);然后使用争论'child_of' => $topid并注意你的depth

完整示例:

   <?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 回答