【问题标题】:Wordpress show (grand)parent, child and grandchildren pagesWordpress 显示(祖)父母、孩子和孙子页面
【发布时间】:2021-03-08 10:02:29
【问题描述】:

我有一个品牌页面(作为父级),其中包含多个产品(子级)、产品组(也是子级)以及产品(孙子)作为页面。我查询了很多,这似乎有点荒谬。有没有更好的方法来实现以下目标?

品牌(父页面)

  • 产品(子页面)
  • 产品组(父页面)
    • 产品(子页面)
    • 产品组(父页面)
      • 产品(子)

我用来查询页面的代码:

<?php

$args = array(
'cat' => 54,
'orderby' => 'menu_order',
'order' => 'ASC',
'hierarchical' => 1,
'post_parent' => $post->ID,
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

  while ( $the_query->have_posts() ) {
      $the_query->the_post();

      get_template_part( 'strips/card-product' );
  }

} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();


$args = array(
'category__not_in' => 54,
'order' => 'ASC',
'orderby' => 'menu_order',
'hierarchical' => 1,
'post_parent__in' => array($post->ID),
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {


    while ( $the_query->have_posts() ) {
        $the_query->the_post();


    // INSIDE PRODUCT GROUP
    if ( in_category('product-groep') ) {

      $args = array(
      'category__not_in' => 54,
      'order' => 'ASC',
      'orderby' => 'menu_order',
      'hierarchical' => 1,
      'post_parent' => get_the_id(),
      'parent' => -1,
      'offset' => 0,
      'post_type' => 'page',
      'post_status' => 'publish'
      );

  // The Query
  $query1 = new WP_Query( $args );

  // The Loop
  while ( $query1->have_posts() ) {
      $query1->the_post();


        if ( in_category('product-groep') ) {

        $args = array(
        'category__not_in' => array(54,8),
        'order' => 'ASC',
        'orderby' => 'menu_order',
        'hierarchical' => 1,
        'post_parent' => get_the_id(),
        'parent' => -1,
        'offset' => 0,
        'post_type' => 'page',
        'post_status' => 'publish',
        'post__not_in' => array(692)
        );

        // The Query
        $query2 = new WP_Query( $args );

        // The Loop
        while ( $query2->have_posts() ) {
            $query2->the_post();

            get_template_part( 'strips/card-product' );


              }

              wp_reset_postdata();

            } else
          {

          } 
        }

        wp_reset_postdata();

        // OUTSIDE PRODUCT GROUP
                                        }
                                        else {


                                        }
        ?>

        <!-- END LOOP 1 -->

      <?


                      }

                  } else {
                      // no posts found
                  }
                  /* Restore original Post Data */
                  wp_reset_postdata();
                  ?> 

                                                                                             

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    使用这个 WP 函数 get_pages() 和 get_page_children()

    function get_child_pages( $parent_page_ID ){
        $all_pages = get_pages( array( 'post_type'=> 'page' ) );
        $child_pages = get_page_children( $parent_page_ID, $all_pages );
        if( !empty( $child_pages ) ){
            $html .= '<ul>';
            foreach ( $child_pages as $key => $child_page ) {
                $html .= '<li>'.$child_page->post_title;
                get_child_pages( $child_page->ID );
                $html .= '</li>';
            }
            $html .= '</ul>';
        }
        return $html;
    }
    
    function list_pages(){
        $html = '';
        $parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'page' ) );
        $html.= '<ul>';
        foreach ( $parent_pages as $parent_page ) {
            $html .= '<li>'.$parent_page->post_title;
            $html .= get_child_pages( $parent_page->ID );
            $html .= '</li>';
        }
        $html.= '</ul>';
        return $html;
    }
    add_shortcode( 'list_pages', 'list_pages' );
    

    【讨论】:

    • 嗨,谢谢.. 但这仅提供页面列表,您无法设置它们的样式(如:特色图片、标题、品牌)。还是我错了?我该如何设计这些样式?
    • 您可以使用 get_pages() 函数,并且可以使用名为“child_of”的参数。