我需要在 wordpress 的产品页面中显示post
的部分。该帖子列表将根据tag names
并在order of priority
之后显示所有帖子。
我的每个产品都有3个标签值。
$variant
$model
$brand
所有带有$ variant标签的帖子都应首先显示
If no post ( with 'tag' => $variant ) OR the total of post < 6
Then get the rest of the post ( with 'tag' => $model )
If no post ( with 'tag' => $model ) OR the total of post < 6
Then get the rest of the post ( with 'tag' => $brand )
到目前为止,我尝试了多种解决方案,例如合并查询或在没有更多帖子时尝试更改查询。但是似乎没有使它起作用。所以我现在回到我的第一个代码,尝试按我想首先显示的标签值对帖子排序。
$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>6, 'tag' => "'.$variant.', '.$model.', '.$brand.'");
$wp_query = new WP_Query( $args );
if ( have_posts()) :
while (have_posts()) : the_post();
get_template_part( 'template-parts/molecule/card', 'vertical' );
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
知道这是否可行吗?
答案 0 :(得分:1)
尝试一下:
$original_query = $wp_query;
$wp_query = null;
$args = array(
'posts_per_page' => 6,
'tag' => $variant
);
$wp_query = new WP_Query( $args );
// Let's check how many posts with the variant tag we find
$count = 0;
// Posts by variant have been found, display them
if ( have_posts() ) {
while( have_posts() ) { the_post();
$count++;
get_template_part( 'template-parts/molecule/card', 'vertical' );
}
}
// We don't have 6 posts yet, let's get more posts by model and/or brand
if ( $count < 6 ) {
$args = array(
'posts_per_page' => 6 - $count,
'tag' => $model
);
$wp_query = new WP_Query( $args );
// Posts by model have been found, display them
if ( have_posts() ) {
while( have_posts() ) { the_post();
$count++;
get_template_part( 'template-parts/molecule/card', 'vertical' );
}
}
// We still don't have 6 posts, let's add some more posts by brand
if ( $count < 6 ) {
$args = array(
'posts_per_page' => 6 - $count,
'tag' => $brand
);
$wp_query = new WP_Query( $args );
// Posts by model have been found, display them
if ( have_posts() ) {
while( have_posts() ) { the_post();
$count++;
get_template_part( 'template-parts/molecule/card', 'vertical' );
}
}
}
}
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
不过,为清楚起见,我将代码重写为此,以避免与原始$wp_query
对象混淆并提高可读性:
$posts_per_page = 6;
$count = 0;
$args = array(
'posts_per_page' => $posts_per_page,
'tag' => $variant
);
$posts_by_variant = new WP_Query( $args );
if ( $posts_by_variant->have_posts() ) {
while( $posts_by_variant->have_posts() ) { $posts_by_variant->the_post();
$count++;
get_template_part( 'template-parts/molecule/card', 'vertical' );
}
}
// We don't have 6 posts, let's get more posts by model and/or brand
if ( $count < $posts_per_page ) {
$args = array(
'posts_per_page' => $posts_per_page - $count,
'tag' => $model
);
$posts_by_model = new WP_Query( $args );
// Posts by model have been found, display them
if ( $posts_by_model->have_posts() ) {
while( $posts_by_model->have_posts() ) { $posts_by_model->the_post();
$count++;
get_template_part( 'template-parts/molecule/card', 'vertical' );
}
}
// We still don't have 6 posts, let's add some more posts by brand
if ( $count < $posts_per_page ) {
$args = array(
'posts_per_page' => $posts_per_page - $count,
'tag' => $brand
);
$posts_by_brand = new WP_Query( $args );
// Posts by model have been found, display them
if ( $posts_by_brand->have_posts() ) {
while( $posts_by_brand->have_posts() ) { $posts_by_brand->the_post();
$count++;
get_template_part( 'template-parts/molecule/card', 'vertical' );
}
}
}
}
// Reset original post object
wp_reset_postdata();