Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm building a site for a magazine using WP and am needing to display a month of posts at a time.

How would I make the next_posts_link(); and previous_posts_link(); functions return results, shorted as one month per page?

If I can't do it via those functions, how would I make the front page display in an archive-like mode?

Thanks!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You have to modify the query that selects the posts to select by month. This bit of code placed in the template will get the page number and subtract that from the current month.

<?php

$page = get_query_var('paged') ? get_query_var('paged') : 1;
$subtractor = $page-1;
$date = date("Y-m-d H:i:s");
$current_month = date('n', strtotime($date.'-'.$subtractor.'months'));
$current_year = date('Y', strtotime($date.'-'.$subtractor.'months'));
query_posts("monthnum=$current_month&year=$current_year&order=DESC");

if (have_posts()) :
while (have_posts()) : the_post(); // rest of the loop below

Ultimately this is probably best hooked into pre_get_posts to keep it out of the template.

You'd have to make your own pagination links as well, something like:

<?php
if($page>1)
    echo '<a href="/page/'.($page-1).'/">next</a>';

echo '<a href="/page/'.($page+1).'/">prev</a>';

of course, you'd have to figure out how far back the posts go to know when to stop providing a previous month link, hmm...

share|improve this answer

I'm not sure if this is what you're looking for; however, this may help out.

<?php
 foreach (get_the_category() as $cat) {
 $arch_date_id = $cat->cat_ID;
 }

 global $post;
 $query = 'numberposts=-1&category=' . $arch_date_id . '&orderby=date&order=DESC';
 $myposts =  get_posts();
      foreach($myposts as $post) {
           $year = get_the_time('Y');
           $groups[] = $year;
 }

 $groups = array_values(array_unique($groups));
 $qi = $groups[0]; 
 $query = 'cat=' . $arch_date_id . '&year=' . $qi . '&orderby=date&order=DESC&posts_per_page=-1';
 query_posts($query);
 echo '<h3>' . $qi . '</h3>';
 while (have_posts()) : the_post();
 ?> 
<? the_date('F j','<h3>','</h3>');?>
<? the_title();?><br />
<? endwhile; ?>
share|improve this answer
    
Uh, not really. This seems to just be a loop that outputs each category by year, completely not what I was asking for... –  aendrew May 7 '11 at 0:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.