WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Pagination in month archive (8 posts)

  1. felixkarlsson
    Member
    Posted 4 years ago #

    Hello!

    I use a month archive and in the month archive i have a pagination. I want to show 10 posts per page (default?), and if there are more than 10 posts in that month i want to show earlier posts. can't get it working. This is my "normal" paginatin, the one i use in index.php:

    <?php
    	$totalposts = $wp_query->found_posts;
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$per_page = get_query_var('posts_per_page');
    	$last_page = ceil($totalposts / $per_page);
    
    	if($last_page > $paged) { next_posts_link('< '); }
    	else { echo '< ';}
    
    	echo "<strong>" .$paged. "</strong>";
    
    	if($paged > 1) { previous_posts_link(' >'); }
    	else { echo ' >';}
    ?>

    This produces "< 1 >", the arrows are links.

    This works fine if you're not in the month archive.

    I use the exact same code in index.php and archive.php, exept for that I use the following code in archive.php:

    <?php
    $month=get_query_var('monthnum');
    $posts=get_posts('monthnum='.$month);
    ?>

    Thank you!

  2. felixkarlsson
    Member
    Posted 4 years ago #

    Anyone? :)

  3. felixkarlsson
    Member
    Posted 4 years ago #

    Can't someone just give me a hint about this?

  4. xdesi
    Member
    Posted 4 years ago #

    You want to show 10 posts per page on all pages or just your archive pages?

    Anyway to set this for all pages you go to your Dashboard, then select Options, then Select Reading and change the number of posts per page to 10.

    If there are more than 10 posts you want to show earlier posts? Not sure what you mean here?

    And what's currently happening on the archive pages, and what would you like to happen?

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    How about:

    <?php
    $month=get_query_var('monthnum');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'monthnum' => $month,
    	'paged' => $paged
    );
    get_posts($args);
    ?>
  6. felixkarlsson
    Member
    Posted 4 years ago #

    Thanks for your replies.

    How do you mean esmi?

    I can fetch the posts, but the problem is the pagination.
    Can you explain how i can build the pagination links from your code there?

  7. esmi
    Forum Moderator
    Posted 4 years ago #

    I have to admit I've not tried this with get_posts but routinely use this approach with query_posts to preserve the default (ie posts per page) pagination. Have you tried using:

    <?php
    $month=get_query_var('monthnum');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args= array(
    	'monthnum' => $month,
    	'paged' => $paged
    );
    query_posts($args);
    ?>

    followed by <?php wp_reset_query(); ?> once you've finished running a Loop?

  8. felixkarlsson
    Member
    Posted 4 years ago #

    It works! :) Thank's a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic