Php 如果没有post或少于3,则向查询中添加其他标记名
我需要在wordpress上的产品页面中创建一个显示Php 如果没有post或少于3,则向查询中添加其他标记名,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我需要在wordpress上的产品页面中创建一个显示post的部分。此帖子列表将根据标签名显示所有帖子,并遵循优先级顺序 我的每种产品都有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' => $
post
的部分。此帖子列表将根据标签名
显示所有帖子,并遵循优先级顺序
我的每种产品都有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
);
$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();
这是可能的,但您需要向我们展示一些代码,以便我们能够提供帮助。“到目前为止,你得到了什么?@cabrerahector是的。我不知道这是否有帮助,因为我回到了我的第一个代码,但它在这里。这里有几个问题:首先,
标记
参数需要是数组,而不是字符串。您不需要将$wp\u query
设置为null
只需使用wp\u reset\u query()
。不要使用$wp\u query
作为变量,使用类似$card\u query=newwp\u query($args)
;tag
参数还接受以逗号分隔的标记段塞字符串,例如。检查,在几个标签下面的显示帖子。@cabrerahector好的,我会的!谢谢分享!
$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();
$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();