如果不在日期范围内,请不要显示$条款?

时间:2014-12-01 12:59:02

标签: php date wordpress args

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

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

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

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

<?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();


}

?>

2 个答案:

答案 0 :(得分:0)

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

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>';
}

还有一个更为抽象的例子this answer 底线是:除非$term->name返回$query->have_posts(),否则不要回显true值。因此,在更多的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;
?>

答案 1 :(得分:0)

<?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();
?>