【问题标题】:get_page_children() not returning all child pagesget_page_children() 不返回所有子页面
【发布时间】:2013-01-08 21:10:56
【问题描述】:

我的 Wordpress 主题中有一个部分,我在其中获取子页面以显示其信息。这就是我现在拥有的:

<?php 
                $my_wp_query = new WP_Query();
                $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

                $staff = get_page_children(8, $all_wp_pages);

                foreach($staff as $s){
                    $page = $s->ID;
                    $page_data = get_page($page);
                    $content = $page_data->post_content;
                    $content = apply_filters('the_content',$content);
                    $content = str_replace(']]>', ']]>', $content);
                    echo '<div class="row-fluid"><span class="span4">'; 
                    echo get_the_post_thumbnail( $page ); 
                    echo '</span><span class="span8">'.$content.'</span></div>';
                } 
        ?>

我有五个应该显示的子页面,但只有三个返回。我在 $staff 上使用 print_r 来查看其他页面是否甚至在数组中,但它们不是。我不确定可能是什么问题。

【问题讨论】:

  • Wordpress 允许多个孩子吗?
  • 这可能是你的问题吗?它只返回当前页面是选择的第一个父级的页面还是什么?

标签: php wordpress


【解决方案1】:

get_page_children()new WP_Query() 没有任何问题。默认情况下,WP_Query 仅返回最后一个 x 创建的页面数。这是对WP_Query 施加的限制。

get_page_children() 只需获取由WP_Query 返回的 pages 数组并从该列表中过滤子页面。根据 WordPress Codex: get_page_children “...不进行任何 SQL 查询来获取孩子。”

要解决这个问题,只需使用:

    $query = new WP_Query( 'posts_per_page=-1' );

您的修复代码:

    <?php 
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => -1));

    $staff = get_page_children(8, $all_wp_pages);

    foreach($staff as $s){
        $page = $s->ID;
        $page_data = get_page($page);
        $content = $page_data->post_content;
        $content = apply_filters('the_content',$content);
        $content = str_replace(']]>', ']]>', $content);
        echo '<div class="row-fluid"><span class="span4">'; 
        echo get_the_post_thumbnail( $page ); 
        echo '</span><span class="span8">'.$content.'</span></div>';
    } 
    ?>

这是一个帮助函数,您可以在需要获取页面子项时调用它

    function my_get_page_children( $page_id, $post_type = 'page' ) {
        // Set up the objects needed
        $custom_wp_query = new WP_Query();
        $all_wp_pages    = $custom_wp_query->query( array( 'post_type' => $post_type, 'posts_per_page' => -1 ) );

        // Filter through all pages and find specified page's children
        $page_children = get_page_children( $page_id, $all_wp_pages );

        return $page_children;
    }

示例

您使用辅助函数进行编码

    foreach(my_get_page_children(8) as $s){
        $page = $s->ID;
        $page_data = get_page($page);
        $content = $page_data->post_content;
        $content = apply_filters('the_content',$content);
        $content = str_replace(']]>', ']]>', $content);
        echo '<div class="row-fluid"><span class="span4">'; 
        echo get_the_post_thumbnail( $page ); 
        echo '</span><span class="span8">'.$content.'</span></div>';
    } 

【讨论】:

    【解决方案2】:

    我遇到了类似的问题 - 看起来 get_page_children 的行为很奇怪......(在我的情况下,对于一个有三个孩子的页面它返回三个,对于另一个有四个孩子的页面它返回零! - 无法解决..)

    我通过使用自定义查询来解决它:

    $params = array(
    'post_type'=>'page',
    'post_parent'=> 8,
    );
    $staff = query_posts($params);
    

    此处类似:http://www.sanraul.com/2010/08/28/get-page-children/

    注意:根据您使用它的位置,您可能还需要 wp_reset_query(); - 否则 query_posts() 可能会破坏您的主循环!

    希望对您有所帮助! - 一个

    【讨论】:

      【解决方案3】:
      $curID = get_the_ID();
      
      query_posts(
          array(
              'post_type'=>'page',
              'post_parent'=>$curID,
              'orderby' => 'menu_order',
              'order' => 'ASC'
          )
      );
      
      echo '<ul>';
      while ( have_posts() ) : the_post();
          echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      endwhile;
      echo '</ul>';
      
      wp_reset_query();
      

      【讨论】:

        【解决方案4】:

        请参阅下面的编辑

        刚刚经历并解决了这个问题,我想我会分享我必须采取的方法。

        最初,我使用一个新的 WP_Query 来抓取所有页面,然后将查询结果提供给 get_page_children():

        $all_pages = new WP_Query( array( 'post_type' => 'page' ) );
        wp_reset_postdata();
        $page_id = get_the_ID();
        
        // pass $all_pages as the second parameter
        $page_children = get_page_children( $page_id, $all_pages );
        
        foreach ( $page_children as $childObj ) {
            echo '<a href="' . $childObj->guid . '">' . $childObj->post_title . '</a>'
        }
        

        不知什么原因,上面的代码对我不起作用。

        相反,我将 $all_pages 的 'posts' 对象作为第二个参数传递,如下所示:

        $all_pages = new WP_Query( array( 'post_type' => 'page' ) );
        wp_reset_postdata();
        $page_id = get_the_ID();
        
        // pass the posts object of $all_pages as the second parameter
        $page_children = get_page_children( $page_id, $all_pages->posts );
        
        foreach ( $page_children as $childObj ) {
            echo '<a href="' . $childObj->guid . '">' . $childObj->post_title . '</a>';
        }
        

        我希望这对某人有帮助!


        编辑: 在使用 get_page_children() 函数遇到更多麻烦后,我选择了一条完全不同的路线,该路线一直在工作以达到相同的最终结果:

        $page = get_queried_object();
        $children = get_pages( 'child_of=' . $page->ID . '&parents=' . $page->ID );
        
        // loop through the child page links
        foreach ( $children as $child ) {
            echo '<a href="' . get_permalink( $child->ID ) . '">' . get_the_title( $child->ID ) . '</a>';
        }
        

        【讨论】:

          【解决方案5】:

          你可以简单地使用

          $args = array(
                  'post_parent' => $parent_id ,
                  'post_type'   => 'page', 
                  'numberposts' => -1,
                  'post_status' => 'publish' 
              );
              $children = get_children( $args );
          

          $children 变量将为您提供父 ID 的所有子页面。如果您使用的是 WP_Query,请确保在代码后重置查询以获取更多信息 https://developer.wordpress.org/reference/functions/get_page_children/

          在您的functions.php文件中使用此代码并使用父页面ID调用函数

              function load_clildren( $parent_id=null ){
              $parent_id  =   ( $parent_id ) ? $parent_id : $post->ID;
          
              $args = array(
                  'post_parent' => $parent_id ,
                  'post_type'   => 'page', 
                  'numberposts' => -1,
                  'post_status' => 'publish' 
              );
              $children = get_children( $args );
              return $children;
          }
          

          函数调用方法

          $ch_pages   =   load_clildren( $parent_id );
          

          或 在父页面中使用

          $ch_pages   =   load_clildren();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-02-17
            相关资源
            最近更新 更多