我正在使用WP Query进行自定义分类-“特色版面”,并尝试解决两个方案:使用高级自定义字段显示具有特定值集的最新特色版面,或显示最新特色版面帖子(即。$stickyToggle == yes
. 前者优先。
现在,下面的代码只输出最新的帖子,但我不知道如何首先检查和输出帖子,如果它同时是一个特色版面和$stickyToggle=是,如果不是,只输出最新的特色版面。代码如下:
<?php
//Getting \'Featured\' Value
$featured_value_cat_query = new WP_Query( array (
\'post_type\' => \'seacoast_values\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'seacoast_value_category\',
\'field\' => \'slug\',
//$term_slug is main category/taxonomy term
\'terms\' => $term_slug,
),
array(
\'taxonomy\' => \'seacoast_value_layout_position\',
\'field\' => \'slug\',
\'terms\' => \'featured-layout\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
),
),
) );
$stickyToggle = get_field(\'seacoast_value_sticky_value\');
//Returning \'Featured\'
if ( $featured_value_cat_query->have_posts() ) {
while ( $featured_value_cat_query->have_posts() ): $featured_value_cat_query->the_post(); ?>
Post Content would be here
<?php
endwhile; //end of \'Featured\' loop
//* Restore original Post Data
wp_reset_postdata();
} //end of if post ?>
最合适的回答,由SO网友:stellarcowboy 整理而成
在回路中运行一次,当发现粘性柱时,将其断开。如果未找到,请回放查询并输出第一个找到的结果:
<?php if ( $featured_value_cat_query->have_posts() ) {
$count = 0;
while ( $featured_value_cat_query->have_posts() ):
$featured_value_cat_query->the_post();
if ( \'yes\' == get_field(\'seacoast_value_sticky_value\') )
{
$count = 1; ?>
// Post Content would be here
<?php break;
}
endwhile; //end of \'Featured\' loop
if ($count == 0) {
$featured_value_cat_query->rewind_posts();
while ( $featured_value_cat_query->have_posts() ):
$featured_value_cat_query->the_post(); ?>
// Post Content would be here
<?php break;
endwhile; //end of \'Featured\' loop
}
} //end of if post
//* Restore original Post Data
wp_reset_postdata();
?>