17

例子:

About
--- Menu 1
--- Menu 2
--- Menu 3
--- Menu 4

如果我在关于页面...我有子页面。但是,如果进入菜单 1,所有页​​面都会消失

我需要的是一直查看父页面

目前我有这个代码

<? if (is_page()) {
    $g_page_id = $wp_query->get_queried_object_id();
    wp_list_pages("depth=4&title_li=&child_of=".$g_page_id."&sort_column=menu_order");
   }
?>

谢谢!

解决

我用这个并且工作正常!

<?php
if ( is_page() ) :
    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' );
    endif;
    if ($children) : ?>
        <div class="title">
            <?php
            $parent_title = get_the_title( $post->post_parent );
            echo $parent_title;
            ?>
            <span></span>
        </div>
        <ul>
            <?php echo $children; ?>
        </ul>
    <?php
    endif;
endif;
?>
4

3 回答 3

14

干得好。作者有点晚了,但人们仍然会来这里寻求答案;-)

<?php 
// determine parent of current page
if ($post->post_parent) {
    $ancestors = get_post_ancestors($post->ID);
    $parent = $ancestors[count($ancestors) - 1];
} else {
    $parent = $post->ID;
}

$children = wp_list_pages("title_li=&child_of=" . $parent . "&echo=0");

if ($children) {
?>

    <ul class="subnav">
        <?php 
            // current child will have class 'current_page_item'
            echo $children; 
        ?>
    </ul>

<?php 
} 
?>
于 2014-04-16T18:36:52.957 回答
9

处理此问题的最简单方法是使用get_children(). 它几乎可以满足您的期望。它返回父页面的子页面。

get_children()基本上是WP_Query类的包装器。

你可以这样使用它...

$child_args = array(
    'post_parent' => 1, // The parent id.
    'post_type'   => 'page',
    'post_status' => 'publish'
);

$children = get_children( $child_args );

如果你想返回当前帖子的孩子,你可以$post->ID作为'post_parent'.

文档get_children()

文档WP_Query

于 2019-10-16T23:33:59.007 回答
0
/* Fetch all child pages of a parent page*/    
$pages = get_pages(
         array (
         'parent'  => '-1',
         'child_of' => 'parent_id', /* Return child of child for current page id */
         'post_type  => 'page',
         'post_status'  => 'publish',
          );
         $ids = wp_list_pluck( $pages, 'ID' );
/* Return page IDs in array format */
/* You can retrieve "post_title", "guid", "post_name" instead of "ID" */

/* Fetch only one level child pages of a parent page */    
         $pages = get_pages(
         array (
                  'parent'  => 'parent_id', 
                  'post_type  => 'page', 
                  'post_status'  => 'publish',
                );
                
         $ids = wp_list_pluck( $pages, 'ID' );
 /* Return page IDs in array format */
 /* You can retrieve "post_title", "guid", "post_name" instead of "ID"
于 2021-11-09T07:22:19.783 回答