Reputation: 15
I'm trying to get pagination working in a page template that I am making. I have already been through multiple threads on this website and I've also been searching through the Wordpress.org documentation (and the comments). Every time I reload my page, there is nothing where the pagination should be.
I've tried both get_posts() and WP_query(). WP_query is the one I believe is needed so that's the one I'll be giving you examples from.
Things I've already tried:
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
'posts_per_page' =>6, 'paged' => $paged,
'posts_per_page=6&paged=' . $paged, 'paged' => $paged,
wp_reset_query();
There are some other things I've tried too but the above are the ones I see recommended most often.
This is my full code in its current state (I've removed some things to help you read it). I have tried to make the first loop with get_posts() instead and that didn't make a difference. I've also tried positioning commands like the wp_reset_query() in several different places. The eventual goal is to have several of these loops on the same page, all except the first with pagination under them (I will be using ajax to allow users to cycle through the different content).
<?php /* Template Name: Home */ ?>
<div>
<?php
/*
First Loop - This one will stay the same on every page. Uses the same categories as the second loop.
*/
$categories = '
category-name1,category-name2
';
$query = new WP_Query( array( 'posts_per_page' =>4, 'category_name' => $categories ) );
?>
<div>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</div>
<div>
<?php
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
$query = new WP_Query( array( 'posts_per_page' =>6, 'paged' => $paged, 'category_name' => $categories, 'offset' => '4' ) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php
endwhile; endif;
pagination_bar();
?>
</div>
Upvotes: 0
Views: 154
Reputation: 26
Try this
<?php /* Template Name: Home */ ?>
<div>
<?php
/*
First Loop - This one will stay the same on every page. Uses the same categories as the second loop.
*/
$categories = array('category-name1' , 'category-name2');
$query = new WP_Query( array( 'posts_per_page' =>4, 'category_name' => $categories ) );
?>
<div>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</div>
<div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query( array( 'posts_per_page' =>6, 'paged' => $paged, 'category_name' => $categories, 'offset' => '4' ) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile;
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => false,
'next_text' => false,
));
}
wp_reset_postdata();
endif;
?>
</div>
Upvotes: 0