I’ve been trying to add numeric pagination to my custom wordpress theme. I have run into a problem where I can’t see any posts on second(or third page). I have my page-archive.php file, index.php, single.php files all set up. It should be everything that a blog site needs? I’m a bit confused what am I missing here? I’ve been trying multiple different options and I tried to modify my page-archive.php page but no luck.
index.php
<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', // 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 // post content goes here ?> <?php endwhile; ?> <?php my_pagination(); ?> <?php else : ?> <?php // no posts found message goes here ?> <?php wp_reset_query(); // add this ?> <?php endif; ?> </div> </div>
functions.php
if ( ! function_exists( 'my_pagination' ) ) : function my_pagination() { global $wp_query; $big = 999999999; // need an unlikely integer 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;
page-archive.php
<?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(); ?>
Advertisement
Answer
It turns out that I don’t need the first half of the code. Feeling dumb. Anyways here is the answer.
<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>