【问题标题】:wordpress archive list of post separated yearly每年分开的帖子的wordpress存档列表
【发布时间】:2014-10-31 10:51:51
【问题描述】:

我有一个时间线帖子页面。 我想按发布年份分开。 例如:

2014
post 1
post 2
post 3

2013
post 1
post 2
post 3

现在发布循环没问题。但我想在每年发布循环的第一行添加年份。 我正在使用下面的代码。如何添加年份?

<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;?>
<?php $media = new WP_Query(array(
                        'post_type' => 'post',
                        'category_name' => 'media',
                        'posts_per_page' => 50,
                        'paged' => $paged
                )); 
?>


<?php if( $media->have_posts()) : while($media->have_posts()) : $media->the_post(); ?>
<span class="app_tl_time">2014</span>
<article class="app_tl_article">
    <span class="app_tl_arrow"></span>
                <div class="app_tl_title">
            <a href="<?php echo get_permalink(); ?>">
                 <?php the_title(); ?>
            </a>
        </div>
</article>

<?php endwhile; ?>
<?php if ($media->max_num_pages > 1) { ?>
    <nav class="prev-next-posts">
        <div class="prev-posts-link">
            <?php // display older posts link
                  echo get_next_posts_link( 'Prev', $media->max_num_pages );  
            ?>
        </div>
        <div class="next-posts-link">
           <?php echo get_previous_posts_link( 'Next' ); // display newer posts link ?>
        </div>
    </nav>
<?php } ?>
<?php else: ?>
<article>
    <p><?php _e('Nothing.'); ?></p>
</article>
<?php endif; ?>
</div><!--app_timeline_grid-->

【问题讨论】:

    标签: php wordpress archive


    【解决方案1】:

    WordPress 有一个名为wp_get_archives() 的内置函数。在你的情况下:

    <?php wp_get_archives( array('type' => 'yearly') ); ?>
    

    阅读有关按年份创建档案的更多信息in the codex

    【讨论】:

    • 感谢您的回答。但我想展示更多的领域。 (缩略图、摘录等)。所以我需要自定义 php 帮助。谢谢
    【解决方案2】:

    只需查看 Template_Hierarchy 页面即可创建副本存档重命名为 2014.php 或 date.php

    wp get archives

    【讨论】:

      【解决方案3】:

      试试这段代码,你可以得到结果

      $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY year DESC" );
      
      foreach ( $years as $year ) {
          // get posts for each year
          $posts_this_year = $wpdb->get_results( "SELECT * FROM wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
      
          echo '<h2>' . $year->year . '</h2><ul>';
      
          foreach ( $posts_this_year as $post ) {
      
              echo '<li>' . $post->post_title . '</li>';        
          }
          echo '</ul>';
      }

      【讨论】:

        猜你喜欢
        • 2013-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 1970-01-01
        • 2013-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多