Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pagination in archive.php Wordpress

I'm working on a paginations for listing multiple articles :

Here's what I wrote so far :

archive.php

<div class="container">
    <div class="row">

    <?php
        $args = array( 
            'posts_per_page' => 4,
            'category_name' => 'actualites',
            'paged' => get_query_var('paged') ? get_query_var('paged') : 1
        );

        $myposts = new WP_Query($args);
    ?>



        <div class="bloc-content col-lg-10 col-md-9 col-sm-12 col-xs-12">
            <h1 class='page-title'><span><?php single_term_title(); ?></span></h1>
            <?php if ( $myposts->have_posts() ) : ?>

                <?php while ( $myposts->have_posts() ) : $myposts->the_post(); ?>

                    <?php get_template_part( 'content', get_post_format() );?>

                <?php endwhile; wp_reset_postdata();?>

            <?php endif; ?>
            <?php if (($myposts->max_num_pages) > 1) { ?>

                <nav class="custom-pagination">
                    <?php echo pagination($myposts); ?>
                </nav>

            <?php } ?>
        </div>
        <div class="sidebar col-lg-2 col-md-3 col-sm-12 col-xs-12">
            <?php get_sidebar(); ?>
        </div>
    </div>
</div>

The pagination is displaying just fine but when I click onto next button, I'm redirecting to a 404 page.

Thanks for your help !

like image 427
Corentin Branquet Avatar asked Dec 19 '22 13:12

Corentin Branquet


1 Answers

Nevermind, I found a solution which fixed my problem :

http://ilikekillnerds.com/2012/11/fixing-wordpress-404-custom-post-type-archive-pagination-issues-with-posts-per-page/

In functions.php insert this code :

function custom_posts_per_page( $query ) {

if ( $query->is_archive('project') ) {
    set_query_var('posts_per_page', -1);
}
}
add_action( 'pre_get_posts', 'custom_posts_per_page' );
like image 115
Corentin Branquet Avatar answered Jan 04 '23 21:01

Corentin Branquet