How i can enable WordPress Posts Pagination - php

How i can enable pagination in my page (template page wordpress)
My Code
<?php
$catquery = new WP_Query( 'cat=2&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<br />
<div class="news"><!-- Start News Box -->
<div class="img_news"><!-- Start Image News -->
<?php
$url_thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<img class="img_thumbs" title="" alt="" src="<?php echo $url_thumb; ?>">
</div><!-- End Image News -->
<div class="title_news"><!-- Start Title News -->
<h2>
<?php the_title(); ?>
</h2>
<div class="details">
<?php the_content_limit(500, "Read More..."); ?>
</div>
</div><!-- End Title News -->
<hr>
</div><!-- End News Box -->
</div>
<?php endwhile; ?>
I am using this but i can't see the pagination bar : example (1-2-3-...-100)
Thanks

you need to add 'paged' attribute in argument($catquery).
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$catargs = array('cat'=>'2','posts_per_page'=>10,'paged' => $paged);
$catquery = new WP_Query( $catargs);
while($catquery->have_posts()) : $catquery->the_post();
//do stuff
endwhile;
$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' => $catquery->max_num_pages,
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
) );

You should add these pagination functions before or after your loop (before or after the while loop):
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
Have a look at Wordpress' documentation here.

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;

WP my pagination function don't show in my custom Template

My pagination function shows in my index page , but when call function in a custom Template page (page-news.php) it don't show !!
functions.php
function numbering_pagination() {
global $wp_query;
$all_pages = $wp_query->max_num_pages;
$current_page = max(1,get_query_var('paged'));
if ($all_pages >1) {
return paginate_links(array(
'base' => get_pagenum_link() . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'mid_size' => 3,
'end_size' => 3,
'prev_text' => 'السابق',
'next_text' =>'التالي'
));
}
}
page-news.php
<?php /* Template Name: news */
get_header(); ?>
<div id="fh5co-blog-section" class="fh5co-section-gray">
<div class="container">
<div>
<div class="text-center heading-section animate-box">
<h3>news</h3>
</div>
</div>
</div>
<div class="container">
<div class="row row-bottom-padded-md">
<?php
$args = array(
'post_type' => 'post',
'category_name'=> 'news'
);
$posts = new WP_Query( $args );
while( $posts->have_posts() ):
$posts->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6">
<div class="fh5co-blog animate-box">
<img src="<?php the_post_thumbnail_url('full')?>" alt="" />
<div class="blog-text">
<div class="prod-title">
<h3><?php the_title()?></h3>
<span class="posted_by"><?php the_time('F jS, Y'); ?></span>
<p><?php the_content('<span class="read-more"> ... more</span>'); ?></p>
</div>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
</div>
</div>
<!-- ------ pagination ------ -->
<div class="pagination-numbers text-center">
<?php echo numbering_pagination() ?>
</div>
<!-- ------ END News ------ -->
How can I solve this?
From the WordPress codex:
<?php
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 5,
'category_name' => 'gallery',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
?>
<!-- the loop etc.. -->
AND:
<?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') ),
'total' => $the_query->max_num_pages
) );
?>
Mind the:
'total' => $the_query->max_num_pages
Also see: https://codex.wordpress.org/Function_Reference/paginate_links

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

Manual Numbered Pagination isn't working in wordpress template file

I have this code which works, showing "Older Entries" and "Newer Entries". However, I wanted to edit this code to show the "1", "2", "3", and so on without left/right arrows or Prev/Nex text. Therefore, I did research and was confused. I can understand what the tutorials such as Wordpress Pagination (Numbered pagination is at bottom), How To Add Pagination To Your WordPress Theme, and Paginate Links, but they didn't say anything about where to put the code in. Only the second tutorial explained in full what I need to do but even so, that way wasn't working...
My code that works:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=9&posts_per_page=9&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="proyectpost">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="innerpost">
<div class="postthumbnail">
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
} ?>
</div>
<div class="posttitle">
<h2><?php the_title(); ?></h2>
</div><!-- .entry-header -->
<div class="postsubtitle">
<div class="datepanel">
</div>
</div>
</div>
</article><!-- #post-## -->
</div>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The second tutorial said to add this code to the functions.php:
// Numbered Pagination
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
and to replace the default pagination with this code
<?php wpex_pagination(); ?>
How do I get it to work to display the numbered pagination in my wordpress template php file?
After researching a bit further, I found some clues which gave me inspiration to figure out where to put the right code so that I can get numbered pagination.
You use this code:
<?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') ),
'total' => $the_query->max_num_pages
) );
?>
to replace:
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
I think what makes me confused was when I was researching, some said to add some code in the functions.php while others said to use other code in certain places. Therefore, when I was reading paginate links, this page wasn't clear on whether or not this new code above should be added to functions.php or to replace "prev/next" links.
Anyway, it works now! :)

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