2014-10-31 48 views
0

我有一個時間線帖子頁。 我想按發佈年份分開這個。 例如:wordpress存檔郵件列表郵件每年分開

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

回答

0

WordPress對此有一個內置函數,稱爲wp_get_archives()。在你的情況下:

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

閱讀更多關於創建歸檔in the codex

+0

謝謝您的回答。但我想要展示更多的領域。 (縮略圖,節選和更多)。所以我需要自定義的PHP幫助。謝謝 – 2014-10-31 11:06:23

0

剛纔看到的頁面Template_Hierarchy只是創建歸檔副本重命名爲2014.php或date.php

enter image description here

OR

0

確定。謝謝大家。

我找到一個解決方案鏈接:http://alex.leonard.ie/2009/08/27/wordpress-grouping-posts-by-monthyear/

作品好。

<?php 
        // Assign the year to a variable 
        $year = get_the_date('Y', '', '', FALSE); 

        // If your year hasn't been echoed earlier in the loop, echo it now 
        if ($year !== $year_check) { 
        echo "<span class='app_tl_time'>" . $year . "</span>"; 
        } 

        // Now that your year has been printed, assign it to the $year_check variable 
        $year_check = $year; 
        ?> 
0

試試這個代碼,就可以得到結果

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