My pagination is not working with WP_Query().
I have a total of three posts. Page 1 correctly displays all three posts. But page 2 displays the same three posts. In fact, there should not be a page 2 since page 1 displays all three posts already.
What could be wrong?
.
Loop in index.php
<?php
$query = new WP_Query('cat=1');
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
the_title();
endwhile;
endif;
?>
<?php my_pagination(); ?>
.
.
Pagination in 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') ),
'prev_next' => True,
'total' => $wp_query->max_num_pages
) );
}
Your query variable in your main loop is $query whereas your query variable in your pagination is $wp_query. Try using $query in place of $wp_query.
So for example:
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
$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') ),
'prev_next' => True,
'total' => $query->max_num_pages
) );
}
Note: your code seems to be missing an endif;
Try with this code for your query:
global $wp_query;
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$wp_query = new WP_Query('cat=1&paged=' . $paged);
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) : $wp_query->the_post();
the_title();
endwhile;
endif;
my_pagination();
In case it still does not work (that might happen depending on the context), try replacing:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
with
$paged = get_query_var('page') ? get_query_var('page') : 1;
Related
PRECONDITION:
I created post type 'comparison'.
I created archive page for 'comparison' post type only.
TASK: I need to create pagination at archive page of 'comparison'.
PROBLEM: I tried to use
<?php echo paginate_links(); ?>
but it doesn't work, pls help.
Try the following code
$query = new WP_Query(
array(
'posts_per_page'=>30,
'post_type'=>'comparison',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
)
);
?>
<?php while ($query -> have_posts()) : $query -> the_post(); ?>
//your code
endwhile;
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) );
}
wp_reset_postdata();
I can add numbered pagination to wordpress fine, the problem is when I click on page two the link for page one doesn't appear to be clickable. The loop still thinks it's on page one.
This is my current code.
<?php query_posts('posts_per_page=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
....
<?php endwhile;
endif; ?>
<?php
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
) );
?>
I am facing a little issue while adding pagination to a custom post type which I've created inside Wordpress. The pagination links are appearing on the template where I've all the posts, inside the custom post type, listed but when I click on the link to view Older posts, it opens the second page but the same posts from the first page are displayed there. Moreover, on the second page, the 'Older posts' link doesn't update to '../page/3', instead it stays '../page/2'. I followed the steps specified here (https://stackoverflow.com/a/18325002/2115001) and modified my code according to the information listed under 'Option 2'. Here's what my code currently looks like:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=3&post_type=medals'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
// loops code here
endwhile;
echo '<nav>';
echo previous_posts_link('« Newer');
echo next_posts_link('Older »');
echo '</nav>';
$wp_query = null;
$wp_query = $temp; // Reset
?>
Do I need to add some code to the functions.php file in order for this to function properly or there's something wrong in my original code? Thanks.
Try below code
$paged = get_query_var('page') ? get_query_var('page') : 1;
$args = array('posts_per_page' => 3,
'paged'=> $paged,
'post_type' => 'medals');
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
// loops code here
endwhile;
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,
'next_text' => __('Next »'),
));
Try this:
<?php
$type = 'portfolio';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
'caller_get_posts'=> 1
);
$temp = $wp_query; // assign original query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query();
wp_query->query($args);
?>
then put your permalinks to the default structure and back to how the structure was.
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article class="">
<div class="">
<h4><?php the_time( 'm' ); ?></h4>
<h4><?php the_time( 'd' ); ?></h4>
</div>
<div class="">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</a>
</article>
<?php endwhile; // End the loop. Whew. ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
//echo esc_url( get_pagenum_link());
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages,
'type'=>'list',
'prev_text' => __('<'),
'next_text' => __('>'),
) );
?>
Hope it will work for you.
I have a child page with custom post query and i need to paginate them. The pagination query itself works, however, the links do not. Currently my page link is like this - /parent-page/child-page/ and the page links goes to /parent-page/child-page/page/2, which returns 404. How can I make this work in this case?
The page link function:
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
) );
}
and a simple custom query in main page
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'press_gallery',
'paged' => $paged,
'posts_per_page' => 30
));
Just follow these steps and you will be done-
1)Install WP-PageNavi plugin and activate it.
2)Now your code should be like this-
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'press_gallery',
'paged' => $paged,
'posts_per_page' => 30
));
$loop = new WP_Query($mypost);
while ($loop->have_posts()) : $loop->the_post();?>
<!--your code here-->
<?php endwhile; wp_reset_query(); ?>
<!--at the end just call this page navi function and you are done-->
<?php wp_pagenavi(array('query' => $loop)); ?>
3)You are done :)
Im Using a post query in wordpress BUT the Pagination is not working,
i don't know whats the problem BUT here is my code and i guess it's correct and no problem with it
it shows that there is pages BUT when i Click on Next Page it refresh the page and don't show any new results just the same page.
Im Using it on Static page to be The Home page of my theme
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_query = query_posts(array(
'post_type' => 'cover', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 1
));
?>
<?php if ( have_posts() ) : ?>
<?php
while ( have_posts() ) : the_post();
?>
<?php endwhile; ?>
///Pagination Function in Functions.php
<?php my_pagination(); ?>
<?php else: ?>
No Results
<?php endif; ?>
Pagination Function
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;
Wordpress Static front page Pagination after a lot of Searches and googling i fix it by using
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array('post_type'=>'cover','posts_per_page'=>2,'paged'=>$paged);
query_posts($args);
I've found a small error in Youssef Subehis solution. It is missing a "d" in "paged" here get_query_var('page'). This fixed the wordpress page pagination problem for me:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page'=>10,'paged'=>$paged);
query_posts($args);
This is referecend in the official wordpress documentation here.