php - 如果没有帖子或少于3个,则在查询中添加其他标签名称
问题描述
我需要在wordpresspost
的产品页面中显示一个部分。此帖子列表将根据并遵循.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();
知道这是否可能吗?
解决方案
尝试这个:
$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();
推荐阅读
- tensorflow - ModuleNotFoundError:没有名为“official.modeling.optimization”的模块
- java - 如何将此 Java 代码正确翻译为 Kotlin
- python - 熊猫合并/重新排列/求和单个数据帧
- javascript - jQuery Pagination 为一页内容显示两个页面按钮
- python - 在python中同时用于无限活动进程
- activemq-artemis - Artemis - 内存和日志之间的消息同步
- flutter - 带有嵌套滚动视图的颤振错误
- android - InputStream 房间类型转换器
- sql-server - Select columns from several tables with count
- unity3d - 为什么我的统一编辑器崩溃了?使用实例化