在年份和月份标题下显示每个分类术语 post

Display each taxonomy term post under a Year and Month heading

我有 CPT - 'newsletters' 和分类法 'newsletter-groups'

我试图在其各自的术语标题下显示每个 post,并在其下方显示所有术语 post,显示它们 post 的月份和年份(由发布日期)。

我以为我有一些工作,但在测试过程中注意到,如果 post 具有相同的 'Month',它将清除其他 post 中的年份和月份相同的术语以及它下面的术语,我有一些图像来显示我的意思和我目前使用的代码。

在显示的图像中,为清楚起见,我将 post 日期附加到文件名,通常它只是文件名。

如果 post 日期月份相同则无效

如果 post 日期月份不同则工作

我该如何解决这个问题?我是否需要通过单独的查询获取所有 post 日期并保存?

任何例子表示赞赏

/*
 * Custom Template: Archive Newsletters
 */


// Remove stuff
remove_action( 'genesis_loop', 'genesis_do_loop' );

// Add our custom loop
add_action( 'genesis_loop', 'newsletter_archive_loop' );

function newsletter_archive_loop() {


  //fetch the terms for the newsletter-groups taxonomy
  $terms = get_terms( 'newsletter-groups', array (
    'hide_empty' => 'true',
    'exclude' => 67,  //excluding parent term ID 'school-newsletters' - only want to show term children of 'school-newsletters'
  ));

     
  // run a query for each term
  foreach( $terms as $term ) {

    // Define the query
    $args = array(
    'post_type'  => 'newsletters',
    'newsletter-groups' => $term->slug ,
    'posts_per_page' => -1,
    );


    // run the query 

    $query = new WP_Query( $args );
    
    if( $query->have_posts() ) { 

      echo '<div class="letters">'; 
          
        // output the term name in a heading tag    
        echo'<h4 class="term-heading">' . $term->name . '</h4>'; 

        while ( $query->have_posts() ) { $query->the_post();
          
          // get current month
          $current_month = get_the_date('F');
          // get attachements from custom field
          $attachment_id = get_field('newsletter_upload');
          $url = wp_get_attachment_url( $attachment_id );
          $title = get_the_title( $attachment_id );
          // get the filesize
          $filesize = filesize( get_attached_file( $attachment_id ) );
          $filesize = size_format($filesize, 2);

            if( $query->current_post === 0 ) { 
                  echo '<h4 class="date">';
                        the_date( 'F Y' );
                  echo '</h4>';
            
              } else { 

                  $f = $query->current_post - 1;       
                  $old_date =   mysql2date( 'F', $query->posts[$f]->post_date ); 

                  if($current_month != $old_date) {
                    echo '<h4 class="date">';
                          the_date( 'F Y' );;
                    echo '</h4>';
                  }

            } ?>

            <li class="letters-file">
              <a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank" rel="noopener noreferrer"><?php the_title(); ?></a><span class="letters-file-sz">&nbsp;(<?php echo $filesize; ?>)</span>
            </li> 

        <?php } // endwhile have posts

      echo '</div>'; // close letters div
    
      // use reset postdata to restore orginal query
      wp_reset_postdata();  
      
    } // end if query have posts

  } // end for each

} // end function 

genesis(); // call genesis framework

删除 if 和 else 这两个条件并尝试只打印 echo ''; the_date('F Y'); echo ''

它只会显示单个 post 日期 - 但不会分组

预期输出

您可以将每个月推送到数组中并检查该月是否已在数组中然后不显示重复月。

/*
 * Custom Template: Archive Newsletters
 */

// Remove stuff
remove_action( 'genesis_loop', 'genesis_do_loop' );

// Add our custom loop
add_action( 'genesis_loop', 'newsletter_archive_loop' );

function newsletter_archive_loop() {

    //fetch the terms for the newsletter-groups taxonomy
    $terms = get_terms( 'newsletter-groups', array (
        'hide_empty' => 'true',
        'exclude' => 67,  //excluding parent term ID 'school-newsletters' - only want to show term children of 'school-newsletters'
    ));
 
    // run a query for each term
    foreach( $terms as $term ) {

        $dates = array();

        // Define the query
        $args = array(
            'post_type'         => 'newsletters',
            'newsletter-groups' => $term->slug ,
            'posts_per_page'    => -1,
        );

        // run the query 
        $query = new WP_Query( $args );
        
        if( $query->have_posts() ) { 

            echo '<div class="letters">'; 
              
                // output the term name in a heading tag    
                echo'<h4 class="term-heading">' . $term->name . '</h4>'; 

                while ( $query->have_posts() ) { $query->the_post();
                  
                    // get current month
                    $current_month = get_the_date('F');
                  
                    // get attachements from custom field
                    $attachment_id = get_field('newsletter_upload');
                    $url             = wp_get_attachment_url( $attachment_id );
                    $title       = get_the_title( $attachment_id );

                    // get the filesize
                    $filesize = filesize( get_attached_file( $attachment_id ) );
                    $filesize = size_format($filesize, 2);

                    if( !in_array(get_the_date( 'F Y' ), $dates ) ){
                        $dates[] = get_the_date( 'F Y' );
                        echo '<h4 class="date">';
                            echo get_the_date( 'F Y' );
                        echo '</h4>';
                    }

                    ?>

                    <li class="letters-file">
                        <a href="<?php echo $url; ?>" title="<?php echo $title; ?>" target="_blank" rel="noopener noreferrer"><?php the_title(); ?></a><span class="letters-file-sz">&nbsp;(<?php echo $filesize; ?>)</span>
                    </li> 

                <?php } // endwhile have posts

            echo '</div>'; // close letters div
        
            // use reset postdata to restore orginal query
            wp_reset_postdata();  
      
        } // end if query have posts

    } // end for each

} // end function 

genesis(); // call genesis framework