【问题标题】:Display each taxonomy term post under a Year and Month heading在年份和月份标题下显示每个分类术语帖子
【发布时间】:2021-06-09 11:59:12
【问题描述】:

我有一个 CPT - 'newsletters' 和 Taxonomy 'newsletter-groups'

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

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

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

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

在发布日期月份不同时工作

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

任何例子表示赞赏

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

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

预期输出

【问题讨论】:

  • 我认为问题是 if($current_month != $old_date) { 这个条件。因为在这种情况下,您的 $current_month 将是 'march' 而 $old 也将是 'march'
  • 我要删除那个吗?
  • 你能补充一下你的逻辑吗?
  • 我从这里抓取了那段代码 - stackoverflow.com/questions/34141251/…
  • 删除 if 和 else 两个条件并尝试仅打印 echo '

    '; the_date('F Y');回声'

    '

标签: php wordpress foreach custom-taxonomy taxonomy-terms


【解决方案1】:

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

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

【讨论】:

  • 这显示了 term 中每个帖子的月份和年份,但不是按月份和年份组合在一起,只是单独显示。
  • Notice: Undefined variable: dates in archive-newsletters.php on line 57 某些术语根本没有日期显示日期
  • 等等..这次我不好,没有复制所有代码,错过了$dates = array();,工作,你是个天才先生!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多