Pagination Issue on Wordpress - wordpress

There are plenty of questions regarding pagination... I've poured through them, and put together the following code (see below). The problem: Some category pages aren't showing any results (a category with only 3 posts). Other's aren't showing all of the posts in that category (one category has 80 posts, but are only showing 60)...
Below is my code...
On my category.php page...
<?php
$currCat = get_category(get_query_var('cat'));
$cat_name = $currCat->name;
$cat_id = get_cat_ID( $cat_name ); // Get cat ID
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'meta_key' => 'ratings_average', // WP-Rating Plugin Rating Value
'orderby' => 'meta_value_num',
'order' => 'DESC',
'cat' => $cat_id,
'posts_per_page' => 10,
'paged' => $paged,
);
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
get_template_part( 'entry' );
endwhile;
//Pagination
post_pagination();
else:
?>Sorry, no results at this time.<?php
endif;
?>
and on my functions.php page...
if ( ! function_exists( 'post_pagination' ) ) :
function post_pagination() {
global $wp_query;
$pager = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $pager, '%#%', esc_url( get_pagenum_link( $pager ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
}
endif;

You don't have to WP query the category posts because your using the category.php file.
Category.php Loop =
<?php if(have_posts()):
while(have_posts()): the_post();
get_template_part( 'entry' );
endwhile;
wp_reset_postdata();
?>
<?php if ( function_exists('post_pagination') ) { post_pagination(); } else if ( is_paged() ) { ?>
<ul class="pagination">
<li class="older"><?php next_posts_link('<i class="glyphicon glyphicon-arrow-left"></i> ' . __('Previous', 'theme')) ?></li>
<li class="newer"><?php previous_posts_link(__('Next', 'theme') . ' <i class="glyphicon glyphicon-arrow-right"></i>') ?></li>
</ul>
<?php } ?>
Then your pagination function:
if ( ! function_exists( 'post_pagination' ) ) {
function post_pagination() {
global $wp_query;
$big = 999999999; // This needs to be an unlikely integer
$paginate_links = paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link($big) ),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'mid_size' => 5,
'prev_next' => True,
'prev_text' => __('<i class="fa fa-angle-double-left" aria-hidden="true"></i>'),
'next_text' => __('<i class="fa fa-angle-double-right" aria-hidden="true"></i>'),
'type' => 'list'
) );
$paginate_links = str_replace( "<ul class='page-numbers'>", "<ul class='pagination'>", $paginate_links );
$paginate_links = str_replace( "<li><span class='page-numbers current'>", "<li class='active'><a href='#'>", $paginate_links );
$paginate_links = str_replace( "</span>", "</a>", $paginate_links );
$paginate_links = preg_replace( "/\s*page-numbers/", "", $paginate_links );
// Display the pagination if more than one page is found
if ( $paginate_links ) {
echo $paginate_links;
}
}
}
The post limit will come from what you set in your reading settings, hope it helps

Related

Custom Post types Pagination shows up but 404s

I can't get pagination to work with custom post types.
It's showing up correctly, when I click it goes to /page/2 and /page/3 etc, and all those give a 404 Page not found error.
index.php
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array('name1', 'name2', 'name3', 'post'),
'posts_per_page' => 4,
'post_status' => 'publish',
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php echo $paged; ?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
// do stuff here
<?php the_excerpt(); ?>
<?php endwhile; ?>
// get pagination
<?php mypagination( $query ); ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
functions.php
the built in the_posts_pagination() doesn't output anything.
function mypagination($query){ ?>
<ul class="pagination">
<?php
$pages = 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' => true,
'type' => 'array',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => '<span class ="fa fa-caret-left" aria-hidden="true"></span><span class="prev-text">Prev</span>',
'next_text' => '<span class="next-text">Next</span> <span class ="fa fa-caret-right" aria-hidden="true"></span>',
'add_args' => false,
'add_fragment' => '',
) );
if (is_array($pages)):
foreach ($pages as $p): ?>
<li class="pagination-item js-ajax-link-wrap">
<?php echo $p; ?>
</li>
<?php endforeach;
endif; ?>
</ul>
<?php
}
If I create a custom API endpoint with the same query/arguments, pagination IS working.
/**
* Custom JSON API endpoint.
* Endpoint URL http://localhost/websitename/wp-json/frontpage/v1
*/
function api_frontpage_posts($data) {
$output = array();
// Get post slug.
$post = get_post($data['parent_id']);
$slug = $post->post_name;
// e.g. 1, 2, 3,...
$paged = $data['page_number'] ? $data['page_number'] : 1;
$query_args = array(
'post_type' => array('name1', 'name2', 'name3', 'post'),
'post_status' => array('publish'),
'posts_per_page' => 4,
'paged' => $paged,
);
// Create a new instance of WP_Query
$the_query = new WP_Query($query_args);
if (!$the_query->have_posts()) {
return null;
}
while ($the_query->have_posts()) {
$the_query->the_post();
$post_id = get_the_ID();
$post_title = get_the_title();
$post_content = get_the_content();
$post_excerpt = get_the_excerpt();
$post_date = get_the_date('l, j F Y');
// Get the slug.
$post = get_post($post_id);
$post_slug = $post->post_name;
// Get image alt.
$alt = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true);
$desc = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_caption', true);
// Get image description.
$description = null;
$thumbnail = get_posts(array('p' => get_post_thumbnail_id(), 'post_type' => 'attachment'));
if ($thumbnail && isset($thumbnail[0])) {
$description = $thumbnail[0]->post_content;
}
// Image data.
$post_image = array(
'id' => get_post_thumbnail_id() ,
'url' => get_the_post_thumbnail_url(),
'caption' => get_the_post_thumbnail_caption(),
'alt' => $alt,
'description' => $description,
);
// Category data.
$categories = get_the_category($post_id);
$post_categories = array();
foreach ($categories as $key => $category) {
$post_categories[] = array(
'name' => $category->name,
'slug' => $category->slug,
'url' => get_category_link($category->cat_ID),
);
}
// Push the post data into the array.
$output[] = array(
'id' => "post" . $post_id, // cannot use numbers, or hyphens between words for Zurb data toggle attribute
'slug' => $post_slug,
'title' => $post_title,
'url' => get_permalink($post_id),
'date' => $post_date,
'content' => $post_content,
'excerpt' => $post_excerpt,
'image' => $post_image,
'categories' => $post_categories
);
}
$result = array(
"next" => (int)$paged === (int)$the_query->max_num_pages ? null : site_url() . '/' . $slug . '/page/' . ($paged + 1) . '/',
"prev" => (int) $paged === 1 ? null : site_url() . '/' . $slug . '/page/' . ($paged - 1) . '/',
// Split the array every 4 items.
"chunks" => array_chunk($output, 4)
);
// Reset the post to the original after loop. otherwise the current page
// becomes the last item from the while loop.
wp_reset_query();
// Return json.
return $result;
}
add_action('rest_api_init', function () {
register_rest_route('frontpage/v1', '/page/(?P<page_number>\d+)', array(
'methods' => 'GET',
'callback' => 'api_frontpage_posts',
));
});
(hooray Reddit)
Someone pointed me to the fix.
Add this to functions.php too, via https://wordpress.stackexchange.com/questions/22528/custom-post-type-pagination-404-fix
add_action( 'parse_query','changept' );
function changept() {
if( is_category() && !is_admin() )
set_query_var( 'post_type', array( 'post', 'your_custom_type' ) );
return;
}

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

Pagination not working with query_posts

I trying to add pagination to my WordPress website, it's working fine with blog page, but with custom category when click any link of pagination URL change, but content still the same.
Custom category code
<?php query_posts('category_name=sport');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('loop'); ?>
<?php endwhile; // конец цикла
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
Pagintion in function.php
function pagination() {
global $wp_query;
$big = 999999999;
$links = paginate_links(array(
'base' => str_replace($big,'%#%',esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'type' => 'array',
'prev_text' => 'Назад',
'next_text' => 'Вперед',
'total' => $wp_query->max_num_pages,
'show_all' => false,
'end_size' => 4,
'mid_size' => 4,
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
));
if( is_array( $links ) ) {
echo '<ul class="pagination">';
foreach ( $links as $link ) {
if ( strpos( $link, 'current' ) !== false ) echo "<li class='active'>$link</li>";
else echo "<li>$link</li>";
}
echo '</ul>';
}
}
Please help
instead of: query_posts('category_name=sport');
try this:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'category_name' => 'sport',
'posts_per_page' => 6,
'paged' => $page
);
query_posts($args);
For further customization check the List of Query Vars
Try this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'category_name' => 'sport'
);
$the_query = new WP_Query( $args );
?>
Hope this helps :)
Please check below code:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'work',
'categories'=>'sports',
'posts_per_page' => 2,
'paged' => $page,
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile;
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
You have to mention post type also as per the example
Hope this work for you.

wordpress wp_Query pagination with custom post_type issue

I'm trying to make pagination for result of two post types which is one custom post type question and the normal post
first of all when I show all result without pagination using 'posts_per_page' => -1; the function works fine
but the problem happen when I try to make pagination, as you can see pagination function works normally except the some last pages of the pagination (I guess which contains question post_type even recent post type of questions appears normally )
here you are my full code
<?php
$my_query = new WP_Query(
array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
)
);
?>
<?php
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<span class="title">
<?php echo get_post_type(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</span>
<br />
<?php endwhile; ?>
<?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' => $my_query->max_num_pages
)
);
?>
How can I make pagination work normally and navigate to all pagination links ?
Replace your code this:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged == "1") {
$args = array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'offset' => 0
);
} else {
$offset = $paged * 5;
$offset = $offset - 5;
$args = array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'offset' => $offset
);
}
$loop = new WP_Query($args);
?>
<?php
if ($loop->have_posts()) :while ($loop->have_posts()) : $loop->the_post();
?>
<span class="title">
<?php echo get_post_type(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</span>
<br />
<?php endwhile; ?>
<div class="pagination-grp">
<?php
$big = 999999999; // need an unlikely integer
//$i=1;
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' => __('<'),
'next_text' => __('>'),
'total' => $loop->max_num_pages
));
wp_reset_postdata();
endif;
?>
</div>
install this free plugin https://wordpress.org/plugins/wp-pagenavi/
then refer below code
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array (
'post_type' => 'post',
'cat' => '53',
'paged' => $paged,
'posts_per_page' => '10',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part('parts/bl`enter code here`ogloop');
}
if(function_exists('wp_pagenavi'))
{
wp_pagenavi( array( 'query' => $query ) );
}
wp_reset_postdata();
} else {
echo 'no posts here';
}
This will show paging and work like a charm for you.
I guess that I figure it out
the solution is to edit the WP_Query args to be a custom number which is not same as site posts_per_page
'posts_per_page' => 11, // 13, or 20 ...
instead of
'posts_per_page' => get_option('to_count_portfolio'),
Why ? I've no idea, maybe the are some kind of conflict
someone can try it and see if that solution works with him/her same as me

Custom Pagination not displaying for custom post types page

I have used a code snippet to build custom pagination:
<?php
if ( ! function_exists( 'procare_paging_nav' ) ) :
/**
* Display navigation to next/previous set of posts when applicable.
*
* #return void
*/
function procare_paging_nav() {
// Don't print empty markup if there's only one page.
if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $GLOBALS['wp_query']->max_num_pages,
'current' => $paged,
'mid_size' => 2,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => '<i class="fa fa-chevron-left"></i>',
'next_text' => '<i class="fa fa-chevron-right"></i>',
'type' => 'list',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation" role="navigation">
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}
endif;
?>
I used the function for blog posts and the pagination appeared for home.php page.
But the pagination is not appearing on my custom post types page.
I have created custom post type called 'ervaringen'.
And in my page-ervaringen.php:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$partnersLoop = new WP_Query( array( 'post_type' => 'ervaringen', 'orderby' => 'post_id', 'posts_per_page' => 10, 'paged' => $paged ) );
while( $partnersLoop->have_posts() ): $partnersLoop->the_post();
?>
<div class="post press-photo">
<div class="image">
<?php the_post_thumbnail(); ?>
</div><!-- image -->
<div class="content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</div><!-- content -->
</div><!-- .press-photo -->
<?php endwhile; ?>
<?php procare_paging_nav(); ?>
<?php wp_reset_query(); ?>
So, if anyone can help me by telling what I am doing wrong that I am missing the pagination on my custom post page.
What other solutions I have tried and didn't work for me:
Changing the function to pass custom query as argument instead of using global query.
<?php
if ( ! function_exists( 'procare_paging_nav' ) ) :
function procare_paging_nav($partnersLoop) {
// Don't print empty markup if there's only one page.
if ( $partnersLoop->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' )
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $partnersLoop->max_num_pages,
'current' => $paged,
'mid_size' => 2,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => '<i class="fa fa-chevron-left"></i>',
'next_text' => '<i class="fa fa-chevron-right"></i>',
'type' => 'list',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation" role="navigation">
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}
endif;
?>
and in page-ervaringen.php
procare_paging_nav($partnersLoop);
Thanks for your time.
The solution was to to pass custom query as argument instead of using global query.
I tried this solution earlier too but didn't get the result as I didn't replace the global query in the link section with the custom ones.
Solved the problem after getting a bit of help from #lucas
Thanks.
Here is the solution. If it might help someone in the future.
In page-ervaringen.php
procare_paging_nav($partnersLoop);
Custom pagination function:
<?php
if ( ! function_exists( 'procare_paging_nav' ) ) :
/**
* Display navigation to next/previous set of posts when applicable.
*
* #return void
*/
function procare_paging_nav($partnersLoop) {
// Don't print empty markup if there's only one page.
if ( $partnersLoop->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $partnersLoop->max_num_pages,
'current' => $paged,
'mid_size' => 2,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => '<i class="fa fa-chevron-left"></i>',
'next_text' => '<i class="fa fa-chevron-right"></i>',
'type' => 'list',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation" role="navigation">
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}
endif;
?>

Resources