如果不在日期范围内,则不要显示$terms


Don't show $terms if NOT in date range?

我有一个存档模板,用于CPT以显示日期范围内的帖子。探针是这样的 - 当一个术语有一个帖子时,它仍然在前端显示"术语"名称,但不在该日期范围内?

我之前有这个模板工作,没有帖子的术语没有显示,但是在添加日期范围参数后,即使该日期范围内没有帖子,术语标题现在也会显示。

我认为它的行为符合预期,因为该术语中有一个实际的帖子,它只是不在正确的日期范围内,所以术语标题显示在其下方没有帖子。

如何修改下面的代码以不显示任何术语标题或不在日期范围内的帖子?

<?php

// Remove stuff
remove_action( 'genesis_loop', 'genesis_do_loop' );
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
// Add our custom loop
add_action( 'genesis_loop', 'newsletter_archive_loop' );
// Add our custom loop
add_action( 'genesis_loop', 'newsletter_archive_loop' );
function newsletter_archive_loop() {
?>

<?php //start by fetching the terms for the year-groups taxonomy
$terms = get_terms( 'year-groups', array(
'hide_empty' => 'true'
 ) );
?>
<?php
// now run a query for each year-group
foreach( $terms as $term ) {
// Define the query
$args = array(
'post_type' => 'newsletters',
'year-groups' => $term->slug ,
'date_query' => array(
    array(
        'after'     => 'July 31st, 2014',
        'before'    => array(
            'year'  => 2015,
            'month' => 8,
            'day'   => 1,
        ),
        'inclusive' => true,
    ),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
// output the term name in a heading tag               
echo'<h2 class="archive-heading">' . $term->name . '</h2>';
$columns = 3;
$increment = 0; 
    // Start the Loop
    while ( $query->have_posts() ) : $query->the_post(); 
    $attachment_id = ( genesis_get_custom_field( 'newsletter_upload_pdf' ) );
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );
?>
    <div class="one-third <?php if($increment % $columns == 0){echo'first';}$increment++; ?>">
            <div class="archive-file">
         <a href="<?php echo $url; ?>"  target="_blank"><?php echo $title; ?></a>
            </div>
        </div>

    <?php endwhile;?>
     <hr/>
<?php
// use reset postdata to restore orginal query
wp_reset_postdata();

}
?>

您在循环外部回显术语名称,而不对WP_Query返回值进行任何检查。根据这个答案,一个简单的解决方法是:

if ($query->have_posts()) {
    echo '<h2 class="archive-heading">' . $term->name . '</h2>';
    while ( $query->have_posts() ) : $query->the_post();
    while ($query->have_posts()) {
        $query->to_post();
        //rest of while loop here
    endwhile;
} else {
    //no else, or:
    echo '<h5>', $term->name, ' - nothing found</h5>';
}

在官方WP文档页面上还有一个稍微抽象的例子。
底线是:除非$query->have_posts()返回true,否则不要回显$term->name值。因此,在更WordPress-y的风格中,您的代码应该是:

<?php
if ($query->has_posts) :
    <h2 class="archive-heading"><?= $term->name; ?></h2>
    <?php
    while ($query->has_posts()) : $query->the_post();
        //do stuff
    endwhile;
else:
    <!-- <?= $term->name; ?> is empty, optional output here -->
endif;
?>
<?php
// Remove stuff
remove_action( 'genesis_loop', 'genesis_do_loop' );
remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
remove_action( 'genesis_entry_footer', 'genesis_post_meta' );
// Add our custom loop
add_action( 'genesis_loop', 'newsletter_archive_loop' );
function newsletter_archive_loop() {
//start by fetching the terms for the year-groups taxonomy
$terms = get_terms( 'year-groups', array(
    'hide_empty' => 'true'
) );
// now run a query for each year-groups
foreach( $terms as $term ) {
    // Define the query
   $args = array(
    'post_type' => 'newsletters',
    'year-groups' => $term->slug ,
    'date_query' => array(
        array(
            'after'     => 'July 31st, 2014',
            'before'    => array(
                'year'  => 2015,
                'month' => 8,
                'day'   => 1,
            ),
            'inclusive' => true,
        ),
    ),
    'posts_per_page' => -1,
);
// run the query 
    $query = new WP_Query( $args );
    if( $query->have_posts() ) { 
    // output the term name in a heading tag               
    echo'<h2 class="archive-heading">' . $term->name . '</h2>';
    $columns = 3;
    $increment = 0; 
        // Start the Loop
        while ( $query->have_posts() ) : $query->the_post(); 
        $attachment_id = ( genesis_get_custom_field( 'newsletter_upload_pdf' ) );
        $url = wp_get_attachment_url( $attachment_id );
        $title = get_the_title( $attachment_id );
    ?>
        <div class="one-third <?php if($increment % $columns == 0){echo'first';}$increment++; ?>">
            <div class="archive-file">
         <a href="<?php echo $url; ?>"  target="_blank"><?php echo $title; ?></a>
            </div>
        </div>
     <?php endwhile;?>
<hr/>
<?php 
}  
}        
    // use reset postdata to restore orginal query
    wp_reset_postdata();
?>