Pagination not working with query_posts - wordpress

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.

Related

Pagination Issue on 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

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

How to add pagination to my custom page template

I just created a new custom page template and called two posts only.
How can I add pagination so that I can the link with at least two of the lastest posts?
I tried this code but it does not work:
<?php
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'category' => 1,
'posts_per_page' => 2,
'paged' => $paged )
);
// The Loop
while ( have_posts() ) : the_post();?>
<div class="news-page-content-wrapper">
<div class="news-page-content">
<h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
<figure><?php the_post_thumbnail(); ?></figure>
<p><?php echo get_the_excerpt();?></p>
Read More&raquo
</div>
</div>
<?endwhile;
// Reset Query
wp_reset_query();
?>
Any help?
Since you're using "the loop" you should use the built in function for displaying pagination.
Here's some examples for you: https://codex.wordpress.org/Pagination
I've updated your sample code to show the default pagination:
<?php
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'posts_per_page' => 2,
'paged' => $paged )
);
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="news-page-content-wrapper">
<div class="news-page-content">
<h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<figure><?php the_post_thumbnail(); ?></figure>
<p><?php echo get_the_excerpt(); ?></p>
Read More»
</div>
</div>
<?php endwhile;
the_post_navigation();
// Reset Query
wp_reset_query();
?>
I sill faced that issue and solved it like that
Pagination function
function post_pagination($paged = '', $max_page = '') {
if (!$paged) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
}
if (!$max_page) {
global $wp_query;
$max_page = isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
}
$big = 999999999; // need an unlikely integer
$html = paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, $paged),
'total' => $max_page,
'mid_size' => 1,
'prev_text' => __('« Prev'),
'next_text' => __('Next »'),
));
$html = "<div class='navigation pagination'>" . $html . "</div>";
echo $html;
}
Query on the custom template
$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => get_option('posts_per_page'),
'order' => 'DESC',
'orderby' => 'date',
'paged' => $paged
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()) :
$loop->the_post();
get_template_part('content', get_post_format());
endwhile;
post_pagination($paged, $loop->max_num_pages);
endif;
wp_reset_postdata();

Wordpress wp_query custom post type query not working on second page

Using below code i'm listing all posts under custom post type people
<?php $loop = new WP_Query(array('post_type' => 'people', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'wpcf-people-sort-order','posts_per_page' => 4, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
); ?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; ?>
Below is my pagination,
<div class="cus-pagi">
<?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' => $loop->max_num_pages
) );
?>
</div>
Also i created a plugin using above code to display list of titles in sidebar. So whenever i access mysite.com/people both(i.e list of custom posts with pagination & sidebar list of post title) of my custom query are working fine.
If i go to second page, sidebar is showing empty.
did anyone know where i'm going wrong ?
You you need to use the Loop with your custom query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'people',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'wpcf-people-sort-order',
'posts_per_page' => 4,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; endif; wp_reset_postdata(); ?>

Wordpress Custom post page with pagination

I have custom post types called clients, need to display 5 clients per page with pagination. The page what i have is page-clients.php
I have used the wp_pagenavi plugin.
I get a perfect navigation list 1,2,3 etc etc but on clicking them takes me to page not found
My Code
$args = array(
'posts_per_page' => 5,
'post_type' => 'clients',
'paged' => get_query_var('page')
);
query_posts($args);
<?php while ( have_posts() ) : the_post(); ?>
.....
<?php endwhile; // end of the loop. ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query();?>
Heres the way you can do it without pagination plugin :) using WP_QUERY instead of query_posts
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'clients',
'paged' => $paged,
'posts_per_page' => 5)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Pagination Like : Prev 1 2 3 Next
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>’YOUR_POST_TYPE’, // your post type name
'posts_per_page' => 5, // post per page
'paged' => $paged,
));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();
// Your code
endwhile;
$total_pages = $data->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 »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
Would you please try above code?

Resources