繁体   English   中英

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

[英]Display each taxonomy term post under a Year and Month heading

我有一个 CPT - '时事通讯' 和分类学 '时事通讯组'

我正在尝试在其各自的术语标题下显示每个帖子,并在其下显示所有术语帖子,显示它们发布的月份和年份(按发布日期)。

我以为我有一些工作,但在测试过程中注意到,如果一个帖子有相同的“月份”,它将消除同一期限内其他帖子的年份和月份以及它下面的条款,我有一些图像要显示我的意思和我目前使用的代码。

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

如果发布日期月份相同,则不工作如果月份相同,则不工作

如果发布日期月份不同,则工作

在此处输入图像描述

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

任何例子表示赞赏

/*
 * 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('FY'); 回声''

它只会显示个人发布日期 - 但不分组

在此处输入图像描述

预期 OUTPUT

在此处输入图像描述

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

/*
 * 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM