显示儿童页面的祖母/父亲

时间:2016-03-02 作者:Dionoh

我有个小问题。

这是我的页面结构:

Ancestor 
   Parent 
     Child 1 
       Child 2 
         Child 3 
            Child 4
我试着做的是,当我在孩子4上时,我想给孩子2看。

当我在看《孩子2》的时候,我想给父母看。

但我不知道怎么做。我使用的代码如下:

function wpb_list_child_pages_popup() { 

    global $post; 

    if ( is_page() && $post->post_parent )

    $childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $post->post_parent . \'&echo=0\' );
else
    $childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $post->ID . \'&echo=0\' );

if ( $childpages ) {

    $string = \'<ul id="child-menu">\' . $childpages . \'</ul>\';
}

return $string;

}

add_shortcode(\'wpb_childpages_popup\', \'wpb_list_child_pages_popup\');

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

你需要看看get_ancestors() 获取页面的顶级父级。请注意,为了可靠性,请使用get_queried_object()甚至更好$GLOBALS[\'wp_the_query\']->get_queried_object())获取单个页面上的当前帖子/页面对象,$post 可能很不可靠

您可以在短代码中尝试以下操作:

$post = $GLOBALS[\'wp_the_query\']->get_queried_object();
// Make sure the current page is not top level
if ( 0 === (int) $post->post_parent ) {
    $parent = $post->ID;
} else {
    $ancestors = get_ancestors( $post->ID, $post->post_type );
    $parent    = end( $ancestors );
}
$parent 将始终保持任何给定页面的顶级父级

编辑-从注释中,如果您需要获得比当前页面高两个级别的页面,您仍然可以使用相同的方法,但不是使用来自“get\\u祖先”(,它是顶级父级)的最后一个ID,我们需要对此进行调整以获得第二个条目

让我们稍微修改上面的代码

$post = $GLOBALS[\'wp_the_query\']->get_queried_object();
// Make sure the current page is not top level
if ( 0 === (int) $post->post_parent ) {
    $parent = $post->ID;
} else {
    $ancestors = get_ancestors( $post->ID, $post->post_type );
    // Check if $ancestors have at least two key/value pairs
    if ( 1 == count( $ancestors ) ) {
        $parent = $post->post_parent;
    } esle {
        $parent = $ancestors[1]; // Gets the parent two levels higher
    }
}
上次编辑-完整代码

function wpb_list_child_pages_popup() 
{
    // Define our $string variable
    $string = \'\';

    // Make sure this is a page
    if ( !is_page() )
        return $string;

    $post = $GLOBALS[\'wp_the_query\']->get_queried_object();
    // Make sure the current page is not top level
    if ( 0 === (int) $post->post_parent ) {
        $parent = $post->ID;
    } else {
        $ancestors = get_ancestors( $post->ID, $post->post_type );
        // Check if $ancestors have at least two key/value pairs
        if ( 1 == count( $ancestors ) ) {
            $parent = $post->post_parent;
        } else {
            $parent = $ancestors[1]; // Gets the parent two levels higher
        }
    }  

    $childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $parent . \'&echo=0\' );

    $string .= \'<ul id="child-menu">\' . $childpages . \'</ul>\';

    return $string;

}

add_shortcode(\'wpb_childpages_popup\', \'wpb_list_child_pages_popup\');

SO网友:Jeffrey von Grumbkow

我不完全理解你的问题是什么,主要是因为你在谈论父母和孩子,但这永远不会显示页面的家长,更不用说祖父母了。

我在这里对您的代码进行了注释,以向您展示代码中到底发生了什么。

function wpb_list_child_pages_popup() { 

    // Get the current WP Post object
    global $post;

    /**
     * If post_type of current WP Post object is \'page\'
     * and the post_parent in the WP Post object is set
     */
    if ( is_page() && $post->post_parent )
        /**
         * Get all wordpress pages that are a child of this pages parent.
         * In other words get THIS page and all it\'s siblings
         */
        $childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $post->post_parent . \'&echo=0\' );
    /**
     * If this page doesn\'t have a parent
     */
    else
        /**
         * Get all wordpress pages that are a child of this page
         */
        $childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $post->ID . \'&echo=0\' );

    if ( $childpages ) {

        $string = \'<ul id="child-menu">\' . $childpages . \'</ul>\';
    }

    return $string;

}

add_shortcode(\'wpb_childpages_popup\', \'wpb_list_child_pages_popup\');
我猜你只是想要一个包含所有页面的列表,这些页面通过(祖辈)父母、(祖辈)子女或兄弟姐妹“连接”。此函数返回一个包含所有这些内容的字符串,无论您在哪个同级上。

function wpb_list_child_pages_popup() {

    // Get the current WP Post object
    global $post;

    if ( is_page() ) {

        /**
         * Get \'first\' parent, the highest ranking post parent which is the ancestor of all.
         * @var array
         */
        $ancestor = array_reverse( get_post_ancestors( $post->ID ) );
        // If there is no ancestor, set ancestor to this page.
        if ( empty( $ancestor ) ) {
            $ancestor_id = $post->ID;
        }
        else {
            $ancestor_id = $ancestor[0];
        }

        // Get childpages from ancestor
        $pages = wp_list_pages( array(
            \'child_of\'  => $ancestor_id,
            \'title_li\'  => \'\',
            \'echo\'      => 0
        ) );

        $string = \'<ul id="child-menu">\';
        // Ancestor is not included in the $pages so add this manually
        $string .= \'<li class="ancestor"><a href="\' . get_the_permalink( $ancestor_id ) . \'">\' . get_the_title( $ancestor_id ) . \'</a></li>\';
        $string .= \'<ul class="children">\';
        $string .= $pages;
        $string .= \'</ul>\';
        $string .= \'</ul>\';

        return $string;
    }
}
add_shortcode(\'wpb_childpages_popup\', \'wpb_list_child_pages_popup\');

相关推荐

如何列出带有摘录的子页,例如[Child-Pages Depth=“1”Excerpt=“1”]

我想构建一个快捷码函数来生成父页面的子页面的HTML列表,并包含摘录。类似这样:<ul> <li> <h3>Child Page Title 1</h3> <p>Excerpt 1</p> <li> <li> <h3>Child Page Title 2</h3> <p>Exc