Skip to content
Advertisement

WordPress custom pagination shows same posts on every page

I have been trying to add custom numeric pagination to my custom WordPress theme. Everything seems good so far but the problem is that every page shows the same 3 posts. Is there something I should consider doing while building my own WordPress blog theme. Right now I have my page-archive.php and single.php file there, do I need something else for this to work? Also filtering with category isn’t working, it keeps sending me back to index.php

Code in my index.php file

<div class="blogitem a">
<?php
            
        //PRINT ONLY Tutvustus
        $lastBlog = new WP_Query('type=post&posts_per_page=3');
            
        if( $lastBlog->have_posts() ):
            
            while( $lastBlog->have_posts() ): $lastBlog->the_post(); ?>
                
                <?php get_template_part('page-archive',get_post_format()); ?>
            
            
            
            
            <?php endwhile;
            
        endif;
        
        wp_reset_postdata();
                
        ?>
        
        <div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }

query_posts(array(
    'post_type'      => 'post',
    'paged'          => $paged,
    'posts_per_page' => 2
));

if ( have_posts() ) : ?>

<?php while ( have_posts() ) : the_post(); ?>

    <?php?>

<?php endwhile; ?>

    <?php my_pagination(); ?>

<?php else : ?>

    <?php ?>

<?php endif; ?>
</div>
        
</div>

Code in my functions.php file

if ( ! function_exists( 'my_pagination' ) ) :
    function my_pagination() {
        global $wp_query;
        $big = 999999999; 

        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages
        ) );
    }
endif;

1st page

3rd page

I modified my page-archives.php file to this code.

<?php
/*
Template Name: Archives
*/
get_header(); ?>

  
<div id="container">
    <div id="content" role="main">

        <?php the_post(); ?>
        <h1 class="entry-title"><?php the_title(); ?></h1>
        
        
         
  
    

    </div><!-- #content -->
</div><!-- #container -->


<?php get_footer(); ?>

Now my filtering with category is working but if I choose second page from the pagination it doens’t show any posts.

Advertisement

Answer

It turns out I am silly and I don’t need the first half of the code anyways.

<div class="pagination">
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }

query_posts(array(
    'post_type'      => 'post', // You can add a custom post type if you like
    'paged'          => $paged,
    'posts_per_page' => 2
));

if ( have_posts() ) : ?>

<?php while ( have_posts() ) : the_post(); ?>

    <?php get_template_part('catalog',get_post_format()); ?> // added template part here and voila it works

<?php endwhile; ?>

    <?php my_pagination(); ?>

<?php else : ?>

    <?php  ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>


</div>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement