【发布时间】: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();
?>
【问题讨论】: