Wordpress pagination on front-page not working - wordpress

I have problem with pagination on a static front-page I have coded. This is not working setting to homepage:
<?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 = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 36,
'category_name' => $cat_slug[3],
'paged' => $paged
));
while ($query->have_posts()) : $query->the_post(); ?>
how to fix them?

You can try as follows -
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
}elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
}else {
$paged = 1;
}
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 36,
'category_name' => $cat_slug[3],
'paged' => $paged
));
while ($query->have_posts()) : $query->the_post();
// Print your post data
endwhile;
// Paginations
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
wp_reset_postdata();

Add this code after your while loop
<div class="nav-links">
<div class="nav-previous"><?php echo get_next_posts_link( __( 'Older Entries', 'yourtextdomain' ), $query->max_num_pages ); ?></div>
<div class="nav-next"><?php echo get_previous_posts_link( __( 'Newer Entries', 'yourtextdomain' ) ); ?></div>
</div>
You can get Older Entries and Newer Entries via this.

Related

I can't get pagination to work in WP query

This is what my current wordpress post query looks like:
<?php
$new_loop = new WP_Query( array(
'post_type' => 'news',
'posts_per_page' => 5
) );
?>
I want to add the following pagination to it:
<?php the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( 'Prev'),
'next_text' => __( 'Next'),
) ); ?>
I googled for various solutions. Everywhere it said to add "paged" to the array, like so:
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;**
$new_loop = new WP_Query( array(
'post_type' => 'news',
'paged' => $paged,**
'posts_per_page' => 5 // put number of posts that you'd like to display
) );
?>
However, this does not work. How can I get the pagination to work in a custom wordpress post query?
I think you're missing the argument: 'current' => max( 1, get_query_var( 'paged' ) )
After you run your loop, you can add this function in your theme(functions.php or elsewhere):
if ( ! function_exists( 'custom_pagination' ) ) {
function custom_pagination( $args = array(), $class = 'pagination' ) {
if ( $GLOBALS['wp_query']->max_num_pages <= 1 ) {
return;
}
$args = wp_parse_args(
$args,
array(
'mid_size' => 3,
'prev_next' => true,
'prev_text' => __( 'Previous', 'theme' ),
'next_text' => __( 'Next', 'theme' ),
'screen_reader_text' => __( 'Posts navigation', 'theme' ),
'type' => 'array',
'current' => max( 1, get_query_var( 'paged' ) ),
//'total' => $the_query->max_num_pages,
)
);
$links = paginate_links( $args );
?>
<nav aria-label="<?php echo $args['screen_reader_text']; ?>">
<ul class="pagination">
<?php
foreach ( $links as $key => $link ) {
?>
<li class="page-item <?php echo strpos( $link, 'current' ) ? 'active' : ''; ?>">
<?php echo str_replace( 'page-numbers', 'page-link', $link ); ?>
</li>
<?php
}
?>
</ul>
</nav>
<?php
}
}
Then finally call it in whatever template you need to:
custom_pagination();

Wordpress posts loop pagination - first page return 125 posts instead of 10 and the rest return 10

I'm trying to show 10 posts per page with pagination on WordPress and the first page return 125 posts instead of 10 and the rest of the pages return 10 posts as requested, please assist :)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$output = array();
global $post;
$args = array('nopaging' => false, 'paged' => $paged, 'posts_per_page' => 10, 'post_type' => 'post', 'order'=> 'DES', 'orderby' => 'date');
$postslist = new WP_Query( $args );
if ( $postslist->have_posts() ) :
while ( $postslist->have_posts() ) : $postslist->the_post();
array_push($output, array("timestamp" => get_the_date('U'),"img_url" => get_the_post_thumbnail_url(), "title" => get_the_title(), "text" => get_the_content()));
endwhile;
wp_reset_postdata();
endif;
Here is the sample code which you can use for the pagination:
<?php
/**
* Looping through the content
*/
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query (array(
'post_type' => 'custom_portfolio',
'orderby' => 'post_id',
'posts_per_page' => 10,
'paged' => $paged,
'order' => 'DESC'
)); ?>
<?php while ($loop -> have_posts()) : $loop -> the_post(); ?>
<?php endwhile; ?><?php wp_reset_query(); ?><?php wp_reset_postdata(); ?>
And here is the part for the pagination:
<!-- Pagination -->
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $loop->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Previous Page', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next Page', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
Here is a good start for you, just adopt as you want.
The sticky posts was the problem, I have excluded the sticky_posts from the query, and it fixed the problem
'ignore_sticky_posts' => 1

The pagination in wordpress not work correctly

I have a problem with pagination for the custom post. I used these code bellow for my site:
The content still show content on loop are correct but when I click next number, it still in first page without jumpping to the next page.
How to fix this issues ?
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'category_name' => 'nha-dat-binh-duong',
'posts_per_page' => 2,
'paged' => $paged
) );
?>
<?php if ( $query->have_posts() ) : ?>
<!-- begin loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
GET CONTENT HERE
<?php endwhile; ?>
<!-- end loop -->
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Tin mới nhất', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( '⏩', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>```
Have you refreshed your permalinks? Simply go to permalinks in WordPress and then refresh the page.

404 error on pagination for custom post type, custom taxonomies

Page numbers and buttons display on a custom post type archive and the archives for custom taxonomies but render 404.
What am I doing wrong?
Here's the query for the custom post type archive:
<?php $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $exec_query = new WP_Query( array (
'post_type' => 'tracker',
'publish_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
) );
}
if ( $exec_query->have_posts() ) { ?><?php global $wp_query; $posts_per_page = $wp_query->query["posts_per_page"]; $posts_found = $wp_query->found_posts; ?><?php while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
This is the pagination code that I am using for the custom post type archive:
<section class="pagination">
<?php global $exec_query;
$big = 999999999;
$translated = __( 'Page', 'theme-name' );
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url (get_pagenum_link( $big )) ),
'format' => 'page/%#%/',
'current' => max( 1, get_query_var('paged') ),
'prev_text' => __('« Previous'),
'next_text' => __('Next »', $exec_query->max_num_pages),
'total' => $exec_query->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.'</span> '
) );
?>
</section>
Here's the query for one of the custom taxonomies:
<?php $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $post_type = get_queried_object(); echo $post_type->rewrite['slug']; $loop = new WP_Query( array(
'post_type' => 'tracker',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'taxonomy1',
'field' => 'slug',
'terms' => $post_type,
),
),
) ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
This is the pagination code for one of the taxonomies:
<section class="pagination">
<?php global $loop;
$big = 999999999;
$translated = __( 'Page', 'theme-name' );
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url (get_pagenum_link( $big )) ),
'format' => 'page/%#%/',
'current' => max( 1, get_query_var('paged') ),
'prev_text' => __('« Previous'),
'next_text' => __('Next »', $loop->max_num_pages),
'total' => $loop->max_num_pages,
'before_page_number' => '<span class="screen-reader-text">'.$translated.'</span> '
) );
?>
</section>

Pagination not working at Template Page

I tried to search right solution for this but I cannot find where I went wrong with code..I want to display pagination on my custom page which uses WP_Query...here is the code:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$frontpageNews = new WP_Query(array(
'category_name' => 'recent-news',
'posts_per_page' => 3,
'paged' => $paged,
'offset' => 1
));
while($frontpageNews->have_posts()) {
$frontpageNews->the_post(); ?>
the_title();
}
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
My posts are showing but not pagination...Sorry if there is existing topic on this, I just couldnt find one that it helped this case, so I came here to ask.
Thanks in advance for your time.
You can get pagination using following code.
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $frontpageNews->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
This is working for me so it should work for you.

Resources