2016-01-21 148 views
0

我想僅在當前月份的wordpress中顯示特定類別中的所有帖子。意味着它將僅顯示當前月份的此類別中的所有帖子,然後分別顯示上個月和下個月的分頁。在wordpress中按月顯示帖子

問題更新 我試過這段代碼。 這是按月顯示帖子,但需要一種方法來顯示此只爲當前月份,然後分頁爲上個月。

<?php 

$blogtime = date('Y'); 
$prev_limit_year = $blogtime - 1; 
$prev_month = ''; 
$prev_year = ''; 

$args = array(
     'posts_per_page' => 20, 
     'ignore_sticky_posts' => 1, 
     'cat' => 1 
); 

$postsbymonth = new WP_Query($args); 

while($postsbymonth->have_posts()) { 

    $postsbymonth->the_post(); 

    if(get_the_time('F') != $prev_month || get_the_time('Y') != $prev_year && get_the_time('Y') == $prev_limit_year) { 

        echo "<h2>".get_the_time('F, Y')."</h2>\n\n"; 

     } 

    ?> 

     <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 

       <?php // your other template tags ?> 


    <?php 

    $prev_month = get_the_time('F'); 
    $prev_year = get_the_time('Y'); 

} 

     ?> 
+0

@Christophvh我已經添加代碼。 –

回答

0

您需要使用這樣的

current_year = date('Y'); 
$current_month = date('m'); 

query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC"); 

如果你不需要使用$ QUERY_STRING變量,另一種方法存在較爲清晰可讀

$args = array(
    'cat'  => 22, 
    'year'  => $current_year, 
    'monthnum' => $current_month, 
    'order' => 'ASC' 
); 
query_posts($args); 

也檢查這個前。

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$today = getdate(); 
$args = array(
    'category_name' => 'news', 
    'year' => $today['year'], 
    'monthnum' => $today['mon'], 
    'paged' => $paged 
); 
query_posts($args); 
?> 

詳細信息請點擊此鏈接 http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters

FOR上一個後一個月

$archive_month = date('m', strtotime('1 month ago')); 
$archive_year = date('Y', strtotime('1 month ago')); 
echo '<a href="' . get_month_link($archive_year, $archive_month) . '">Last month\'s posts</a>'; 

後按月份 - 按照這個 http://codex.wordpress.org/Function_Reference/get_month_link

+0

這是顯示當前月份後,但沒有顯示分頁?我怎麼能得到前一個和下一個月的鏈接? –

+0

好的檢查更新ans –