Pagination on Home Page (WordPress) - php

I'm having an issue trying to get pagination to work on the homepage of a site I'm working on.
The "Older" and "Newer" links show correctly, and it updates the url to reflect the page number but the posts content remains the same as it does on the first page when flipping through the pages. Here is the code I'm using, simplified of course:
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
$get_featured_posts = new WP_Query( array(
'posts_per_page' => 9,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'paged' => $paged,
'offset' => 5
) );
while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
<h3 class="entry-title entry-added">
<?php the_title(); ?>
</h3>
<p class="short_description"><?php echo short_description('...', 16); ?> </em></p>
<p class="read_more">Read More</p>
<?php
endwhile;
?>
<?php
// Reset Post Data
wp_reset_query();
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
Can't seem to figure out what the problem is, any help would be appreciated.

try to use wp_reset_postdata(); instead of wp_reset_query(); and also updated condition for $paged.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$get_featured_posts = new WP_Query( array(
'posts_per_page' => 9,
'post_type' => 'post',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'paged' => $paged,
'offset' => 5
) );
while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
<h3 class="entry-title entry-added">
<?php the_title(); ?>
</h3>
<p class="short_description"><?php echo short_description('...', 16); ?> </em></p>
<p class="read_more">Read More</p>
<?php
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
?>

Please read this and insert that code of pagination on your page and remove newer and older links
https://www.elegantthemes.com/blog/tips-tricks/how-to-add-pagination-to-wordpress

Figured out the problem thanks to #milo on the WordPress exchange. Turns out there are special considerations when using "offset" and pagination. Check this link for more details.

Related

Why isnt my pagination working in the while post loop?! Help would be much appreciated, new to PHP

I have this code down below, showing 16 posts and on the next page the next 16 but right now it's not getting new posts on the second + page. It is showing the same first 16 that it shows on the first page. I don't know where I went wrong and why its not communicating with the while loop?
$cat = get_the_category();
$cat_name = esc_html($cat[0]->name);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => $cat_name,
'posts_per_page' => 16,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'offset' => 5
);
$all_catPosts = null;
$all_catPosts = new WP_Query($args);
if ($all_catPosts->have_posts()) {
?>
<!-- 16 category container with the next page pagination button -->
<div class="min-h-screen flex items-center justify-center">
<div class="grid grid-cols-4">
<?php
while ($all_catPosts->have_posts()): $all_catPosts->the_post();?>
<div class="p-5 rounded big_cat_container">
<!-- // all content, title, meta fields-->
<section><a class="thumbnail_img" href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a></section>
<h4 class="post_title"><?php the_title();?></h4>
</div>
<?php endwhile;?>
</div><!--/ end of grid-->
<!-- //pagination buttons -->
<?php
$previousLink = get_previous_posts_link();
$nextLink = get_next_posts_link();
$hasNextPage = $previousLink || $nextLink;
if ($hasNextPage):?>
<nav class="pagination" role="navigation">
<?php if($previousLink || $paged > 1 ) { ?>
<div class="page-btn nav-next">
<button class="previous-btn"><?php previous_posts_link( 'Previous Page', ); ?></button>
</div>
<?php }
if($nextLink) { ?>
<div class="page-btn nav-previous">
<button class="next-btn">
<?php next_posts_link( 'Next Page ', $the_query->max_num_pages );?>
</button>
</div>
<?php } ?>
</nav>
<?php endif;?>
<!-- end pagination -->
</div><!--/ end of container-->
<?php } ?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php
$cat = get_the_category();
$cat_name = esc_html($cat[0]->name);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => $cat_name,
'posts_per_page' => 16,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'offset' => 5
);
$all_catPosts = null;
$all_catPosts = new WP_Query($args);
if ($all_catPosts->have_posts()) {
?>
<!-- 16 category container with the next page pagination button -->
<div class="min-h-screen flex items-center justify-center">
<div class="grid grid-cols-4">
<?php
while ($all_catPosts->have_posts()): $all_catPosts->the_post();?>
<div class="p-5 rounded big_cat_container">
<!-- // all content, title, meta fields-->
<section><a class="thumbnail_img" href="<?php the_permalink();?>"><?php the_post_thumbnail();?></a></section>
<h4 class="post_title"><?php the_title();?></h4>
</div>
<?php endwhile;?>
</div><!--/ end of grid-->
<!-- //pagination buttons -->
<?php
$previousLink = get_previous_posts_link();
$nextLink = get_next_posts_link();
$hasNextPage = $previousLink || $nextLink;
if ($hasNextPage):?>
<nav class="pagination" role="navigation">
<?php if($previousLink || $paged > 1 ) { ?>
<div class="page-btn nav-next">
<button class="previous-btn"><?php previous_posts_link( 'Previous Page', ); ?></button>
</div>
<?php }
if($nextLink) { ?>
<div class="page-btn nav-previous">
<button class="next-btn">
<?php next_posts_link( 'Next Page ', $all_catPosts->max_num_pages );?>
</button>
</div>
<?php } ?>
</nav>
<?php endif;?>
<!-- end pagination -->
</div><!--/ end of container-->
<?php } ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 16,
'paged' => $paged
);
if ( $the_query->have_posts() ) :
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
// next_posts_link() usage with max_num_pages.
next_posts_link( __( 'Older Entries', 'textdomain' ), $the_query->max_num_pages );
previous_posts_link( __( 'Newer Entries', 'textdomain' ) );
// Clean up after the query and pagination.
wp_reset_postdata();
else:
?>
<p><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ) ); ?></p>
<?php
endif;

Tag template instead of 'post' in the following loop

I'm using the code (loop) below for the blog post grid displaying. I want the following structure for my tag.php as well (the post by the tag instead of all blog posts). But I don't know how to do it. Could you, please, check it?
I can't remember how I solved the following problem in the past but I'm at a 100% sure that I found the solution previously.
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="item col-sm-4">
<div class="well">
<h2><?php the_title(); ?></h2>
<span class="date"><?php echo get_the_date("j.n.Y"); ?></span>
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Suite</a>
</div>
</div></div>
<?php endwhile; ?>
</div>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="next-posts-link">
<?php echo get_previous_posts_link( '< Page précédente' ); ?>
</div>
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Page suivante >', $custom_query->max_num_pages ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Please check WP_Query with Tag Parameter
https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
example:
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date', // modified | title | name | ID | rand
'tag' => 'tag1,tag2'
);

Next/Prev links not working on Wordpress pagination

I have a static front page on my wordpress site and am trying to get pagination working.
The pagination actually works if I manually go to the address (/2/, /3/ etc) but the next_posts_link and previous_posts_links don't work at all.
Could anyone pinpoint what my error is here?
<?php $args = array(
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1,
'category__in'=> $cat,
'paged' => $paged,
);
$featured_query = new WP_Query($args);
while ($featured_query->have_posts()) : $featured_query->the_post();
?>
<?php get_template_part( 'template-parts/content', get_post_format() ); ?>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts', $featured_query->max_num_pages ); ?></div>
<?php if(is_paged()) { ?>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts', $featured_query->max_num_pages ); } ?></div>
You should check your scripts , it maybe and actual script file that you can't reach , inspect that certain element.
Please try below code
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args_blog = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => get_option('posts_per_page ')
);
$query_blog = new WP_Query( $args_blog );
if($query_blog->have_posts()) : while($query_blog->have_posts()) : $query_blog->the_post();
?>
<div class="pagination">
<?php
$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_text' => __('Prev'),
'next_text' => __('Next'),
'total' => $query_blog->max_num_pages
) );
?>
</div>
<?php
else :
get_template_part( 'content', 'none' );
endif;
?>
Try this code:
You should call <?php wp_reset_postdata(); ?> after post link see updated code:
<?php
$args = array(
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1,
'category__in'=> $cat,
'paged' => $paged,
);
$featured_query = new WP_Query($args);
while ($featured_query->have_posts()) : $featured_query->the_post();
?>
<?php get_template_part( 'template-parts/content', get_post_format() ); ?>
<?php endwhile;?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts', $featured_query->max_num_pages ); ?></div>
<?php if(is_paged()) { ?>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts', $featured_query->max_num_pages ); } ?></div>
<?php wp_reset_postdata(); ?>

Custom wordpress loop pagination not working

I have a website and i have a few loops from different categories on one page. the posts display like i want them to. At the bottom of the page i want to display all of the posts from the site. i have also got this working correctly. however the posts wont paginate, when i click on "newer posts" i am taken from this url: sitename/blog/ to sitename/blog/page/2/ but there is nothing found there and im getting an error
"Oops! That page can’t be found . which is found in my 404 page."
for my first two loops i have used the following code (with different queries)
<?php query_posts('cat=2&showposts=3'); ?>
however from research i have found this is the wrong way to do it and i should be querying posts like:
$the_query = new WP_Query($query_args);
So i have changed the query at the bottom of the page to this format, and temporarily removed the top two (wrong) queries. However im still getting the same error.
Could someone please help. My php knowledge is limited and this is stretching me. the full code i am using to display the post and paginate is:
<?php
$query_args = array(
'cat' => '3',
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => $paged
);
// Get current page and append to custom query parameters array
$query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query($query_args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="articleThirdBlock">
<div class="post__thumbnail post__thumbnail--marginTop">
<?php the_post_thumbnail(array(400,250)); ?>
</div><!-- /.post__thumbnail -->
<h2 class="other__title"><?php the_title(); ?></h2>
</div><!-- /.articleThirdBlock -->
<?php endwhile; endif; ?>
<?php wp_reset_postdata();
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
Custom Pagination Like « prev 1 2 3 next »
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'post', // Your post type name
'posts_per_page' => 6,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="articleThirdBlock">
<div class="post__thumbnail post__thumbnail--marginTop">
<?php the_post_thumbnail(array(400,250)); ?>
</div><!-- /.post__thumbnail -->
<h2 class="other__title"><?php the_title(); ?></h2>
</div><!-- /.articleThirdBlock -->
<?php
endwhile;
$total_pages = $loop->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' => __('« prev'),
'next_text' => __('next »'),
));
}
}
wp_reset_postdata();
?>

Wordpress Static frontpage search pagination

I have a static frontpage on my wordpress website, displaying my posts. Currently it shows 10 posts and I want to make it possible to click "next page" to see the next 10 posts in the query and so on. I have tried getting the pagination to work, but it looks like I'm unable to connect it with my search query.
Here is my query:
<?php
query_posts(
array( 'post_type' => 'post',
'order' => 'DESC',
'meta_key' => 'cf_votes',
'orderby' => 'meta_value_num',
'posts_per_page' => 10
)
);
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
And here is the pagination I have tried:
<?php
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
) );
?>
So basically I'm able to see the first 10 posts perfectly. But no alternative to see more posts is shown.
Wordpress Codex is pretty comprehensive on the Pagination problem, I recommend you to see this:
http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
There's a section specifically for the Static Front Page.
Something like:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$args = array(
'posts_per_page' => 3,
'paged' => $paged
);
query_posts($args);
?>
<?php if ( have_posts() ) : ?>
<!-- Add the pagination functions here. -->
<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post(); ?>
<!-- the rest of your theme's main loop -->
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Categories