Wordpress numbered Pagination & updating the current page using query_posts - php

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
) );
?>

Related

How To Make Pagination For Custom Post Type In Wordpress?

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();

How to fix wordpress pagination redirect?

I am using pagination in wordpress category.php page. everthing is fine but when I click to next page i got following error
Template is missing. Standalone themes need to have a index.php template file. Child themes need to have a Template header in the style.css stylesheet.
My url is some thing like
https://test.com/category/cat_name/page/2/
Here is my code:
$cat_name = $category->name;
$cat_id = get_cat_ID( $cat_name );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=1&post_type=post&paged='.$paged.'&cat='.$cat_id);
while ($wp_query->have_posts()) : $wp_query->the_post();
/// loop here
<?php endwhile; ?>
Code for pagination:
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo '<div class="custom-pagination">';
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
echo '</div>';
?>
Should I add some code in htaccess file or function.php for redirect in category.php?
I got the answer by my self. I need to change the Blog pages show at most in reading section of Dashboard. I set same number of showposts i.e 1 in Blog pages show at most.

Pagination not working while using custom query

I have a website where On the home page I show all the posts of the website but now the requirement is to show all the posts excluding 3 Categories, but when I updated my WP_Query accordingly, Now Pagination started showing posts in loop, so Right now I am showing 12 posts in one page, so after 12 posts it repeats from the first post again and so one (right now my pagination is linked with infinite scroll but even if I disable infinite scroll, and switch to normal pagination, the issue still remains.
Below is my custom query
$fullquery = new WP_Query( 'cat=-12,-34,-56' );
while ($fullqury->have_posts()) : $fullqury->the_post();
And Below is my Pagination Code
<?php
$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' => $fullquery->max_num_pages
) );
?>
Try this
I'm not use the WP native pagination.
Actually I emptied the code in pagination.php
In your template.php, where you want to place your pagination
<div class="row">
<div class="col-md-12">
<div class="pagination">
<?php pagination_bar( $loop ); ?>
</div>
</div>
</div>
In the same template in the loop add :
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
And in your function.php (theme child)
// Pagination
function pagination_bar( $custom_query ) {
$total_pages = $custom_query->max_num_pages;
$big = 999999999;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
));
}
}
// END Pagination
It works for me

adding pagination to a custom post type in wordpress

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.

Pagination not working in WP_Query()

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;

Categories