0

I've been looking around for a solution to displaying a separate amount of posts specifically for the archive pages, and thought I had came up with some working code, however I see it messes up pagination. Basically page 2 of the archive displays the same posts as the archive front page. Link: http://hvamgolfklubb.no/category/ukategorisert/

Code:

<?php 

            /* Sets the amount of posts to display for $querycat */
            $querycat = new WP_Query( 'posts_per_page=4' ); ?>

        <?php
        /* Start the Loop */
        while ( $querycat->have_posts() ) : $querycat->the_post(); /* Display $querycat amount of posts */

            /* Include the post format-specific template for the content. If you want to
             * this in a child theme then include a file called called content-___.php
             * (where ___ is the post format) and that will be used instead.
             */
            get_template_part( 'content', get_post_format() );

        endwhile;

        wpbeginner_numeric_posts_nav();
        ?>

Any help is appreciated. I know it's not the numeric pagination that messes up as it behaved like this before implementing that as well. I'm still quite new to PHP, but improving every day, so please do share a better solution if you know of any.

6
  • 2
    Have you seen How to fix pagination for custom loops?
    – fuxia
    Commented May 19, 2015 at 17:15
  • try changing $querycat to $wp_query
    – shanebp
    Commented May 19, 2015 at 17:18
  • I tried the code in your link toscho, and now it displays no posts at all. It became a bit heavy to read through, so I don't know exactly what I'm doing wrong.
    – Thomas
    Commented May 19, 2015 at 18:18
  • I mean an easy workaround would be to use the custom query for the front page without pagination and use the main query for those archive pages that need pagination. Would love to learn the proper way though.
    – Thomas
    Commented May 19, 2015 at 18:24
  • your query loads the 4 most recent posts regardless of what page you're on, because that is exactly what you are asking for with those query parameters. have a look at pagination parameters for WP_Query in codex.
    – Milo
    Commented May 19, 2015 at 19:25

1 Answer 1

2

If I understand you correctly you just want to display a different amount of posts at archives, for this you don't need a custom secondary query at all. You can control it via pre_get_posts hook, either making use of posts_per_page or posts_per_archive_page parameter.

Example for pre_get_posts

function wpse188786_different_number_of_posts_for_archive_ppp( $query ) {
    if ( !is_admin() && $query->is_main_query() ) {
        if ( $query->is_archive ) {
            $query->set( 'posts_per_page', 4 );
        }
    }
}
add_action( 'pre_get_posts','wpse188786_different_number_of_posts_for_archive_ppp' );

Example for posts_per_archive_page

function wpse188786_different_number_of_posts_for_archive_ppap( $query ) {
    if ( !is_admin() && $query->is_main_query() ) {
        // no $query->is_archive check needed, because posts_per_archive_page
        // does override, when $query->is_archive or $query->is_search is true
        $query->set( 'posts_per_archive_page', 4 );
    }
}
add_action( 'pre_get_posts','wpse188786_different_number_of_posts_for_archive_ppap' );
3
  • Wow, you wouldn't believe how much googling and searching I've done to find something like this. Can't believe I've somehow not found it. Anyway, thanks! It made everything work as I want for now. Would I be able to tweak this to have it work for specific pages? Down to either body class/ID, or template file.
    – Thomas
    Commented May 19, 2015 at 21:44
  • EDIT: Obviously, I know I can based on template files. Just overlooked that piece of code when writing this.
    – Thomas
    Commented May 19, 2015 at 21:55
  • With the right combination of conditional tags you can target a lot of things. @Thomas Commented May 20, 2015 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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