• Hi

    Is there a way to modify the loop so that a page of posts is limited by the month that is being viewed, regardless of if you are viewing a category listing of posts or just the blog-roll?

    I am transferring a static news site with several years of content into a cms so would like to try and maintain the same views as set in the current static site.

    I tried to modify the loop with get_lastpostdate() but I cannot get the previous/next navigation to show anything else but the get_lastpostdate().

    <?php	$lastpost = strtotime(get_lastpostdate());
    		$lastmonth = date('m', $lastpost);
    		$lastyear = date('Y', $lastpost);
    		$args=array(
    			  'post_type' => 'post',
    			  'post_status' => 'publish',
    			  'year'=>$lastyear,
    			  'monthnum'=>$lastmonth,
    			  'order'=>'DESC',
    			  'posts_per_page' =>50
    		);
    		query_posts($args);
    ?>

    The reason I used get_lastpostdate() is that there could be instances where there are no posts for a month. I thought that this would at least fix this issue for the initial view.

    I’m a bit stuck on this so any advice on this would be greatly appreciated.

    I do have additional questions, such as how to change the paged url navigation from %category%/page/2 to something like %category%/%month-year%. But I think the pagination is much more critical.

    Are there any resources that I should read up on to try and understand this better?

    Thanks

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    When you use query_posts() you are essentially discarding the original query and replacing it with your own. Besides being inefficient, it makes pagination difficult, you essentially need to manage it yourself.

    The best approach is to hook ‘pre_get_posts‘ and modify the main query to meet your needs. The added benefit of this approach is the pagination should work automatically without any special coding.

Viewing 1 replies (of 1 total)
  • The topic ‘navigate and paginate by month’ is closed to new replies.