dongtan7639 2013-06-12 22:22
浏览 6

在WP_Query中显示2个级别?

Currently running this

$args = array(
    'post_type'       => 'page',
    'posts_per_page'  => 1,
    'post_parent'     => 1743,      
    'meta_query'      => array(
        array(
            'key'      => 'sticky',
            'value'    => '1',
            'compare'  => '=='
        )
    )
);

$the_query = new WP_Query( $args );

I have pages nested 2 deep in the post_parent but it only pulls one level deep. Any way of extending it to subpages down below the immediate children?

  • 写回答

1条回答 默认 最新

  • duanjitong7226 2013-06-13 04:20
    关注

    I haven't tried this, but it should work: While you're in the Loop, for each of the 2nd-tier posts (i.e., direct children, which you are currently able to reach) capture the current post ID like so

    $level2_parent_ID = $post->ID;
    

    Then use WP_Query to start a query within the query, using $sub_parent_ID as the value for post_parent. This should pull up children of the children.

    As an example, to continue where you left off...

    $the_query = new WP_Query( $args );
    // The Loop
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
                $level2_parent_ID = $post->ID;
                $level2_args = array(
                    'post_type'       => 'page',
                    'posts_per_page'  => 1,
                    'post_parent'     => $level2_parent_ID,      
                    'meta_query'      => array(
                        array(
                            'key'      => 'sticky',
                            'value'    => '1',
                            'compare'  => '=='
                        )
                    )
                );
    
                $level2_query = new WP_Query($level2_args);
                // The Loop-within-a-loop
                    if ( $level2_query->have_posts() ) {
                    while ( $level2_query->have_posts() ) {
                    $level2_query->the_post();
                    echo '<li>' . get_the_title() . '</li>';
                    }
                    }
                //plus whatever else you want to do in the outer loop
        }
    } 
    /* Restore original Post Data */
    wp_reset_postdata();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制