Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Group posts by month on category page WordPress

  • REFUNDED

For a specific category I need to group posts by month and display them in a grid. I achieved this with the code below, however I need to adjust the pagination so full months are shown and months are not split over two pages.

$page_num = $paged
$posts_per_page = 40;
if($pagenum='') $pagenum=1;
query_posts('cat='.get_query_var('cat').'&posts_per_page='.$posts_per_page.'&paged='.$page_num); ?>
<div id="content">
<?php if( have_posts() ) : $count=0; ?>
<?php $previous_month = ""; ?>
<?php while ( have_posts() ) : the_post(); $count++; ?>
<?php $post_month = get_the_date('mY'); ?>
<?php if( $post_month != $previous_month ) : $count=1; $previous_month = $post_month; ?>
<div class="clear"></div>
<h3 class="subheader"><?php echo ucfirst(get_the_date('F Y')) ?></h3>
<?php endif; ?>
<?php if($count==1) $class='left'; if($count==2) $class='middle'; if($count==3){$class='right clearfix'; $count=0;} ?>
<div <?php post_class('grid-item '.$class); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img class="border small left" src="<?php thumb_src(get_post_meta(get_the_ID(), 'thumb_url', true), THUMB_SMALL_WIDTH, THUMB_SMALL_HEIGHT); ?>" alt="<?php the_title(); ?>" width="<?php echo THUMB_SMALL_WIDTH; ?>" height="<?php echo THUMB_SMALL_HEIGHT; ?>"/></a>
<div class="meta small">
<h3 class="title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php short_text(the_title_attribute('echo=0'), 30); ?></a></h3>
<div class="date"><?php echo get_the_date('j-n-Y'); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php paginate(); ?>
<?php else : ?>
<p>Geen posts.</p>
<? endif; ?>
</div>


The loop checks if the current post is in a different month as the previous. When this is true, a new header is printed and a new grid is shown. My question is; how to adjust the posts_per_page so that full months are shown and pagination is maintained?

Answers (3)

2010-08-10

Erez S answers:

$posts_per_page = -1;
will display all posts, and all you have to do is to add "monthnum" parameter to the query


jedw comments:

How to maintain pagination?

2010-08-10

Ehthisham tk answers:

if you give $posts_per_page= -1
it will display infinite no.of posts in a single page ,no worries about pagination .
if you want to remove pagination only for a particular category you can do this
store category id to a variable <strong>$cat_id </strong> and
<?php
foreach((get_the_category()) as $category) {

$cat_id = $category->cat_ID ;

}
?>
check for condition
<?php
if( $cat_id!="your particular category_id"){
luckytv_paginate();
}
?>


jedw comments:

Thank you for your reply. Ofcourse just removing pagination is easy. I want to use pagination here and let it handle the the months well as I described in the question.


Ehthisham tk comments:

monitor change in months and and when a change occures increase a var value to 1
when two or three changes occures
apply pagination


Ehthisham tk comments:

when the value is 12 decerase to 0


Ehthisham tk comments:

i donno how to delete ▲ that;sorry
once the value becomes 2 or 3 apply pagination and make the value 0 ,continue looping

2010-08-10

Mykyta Savchenko answers:

It would be cool if you will show a screenshot or simple scheme )

If I've understood you correctly the task is to display post in that order:

<blockquote>January
post1
post2
post3

February
post4
post5

March
post6

August
post7</blockquote>

and to add pagination if amoun of posts is more than 40?


jedw comments:

I have attached a sketch of the grid. You have understood how I want to display the posts but I have already achieved that with the code I pasted in the question. Currently, pagination is added when there are more then 40 posts. However, I want to achieve to add pagination every X (2 or 3) months. So the first page shows January, February and March, the second page (if there are older posts) show April, May, etc.