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

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

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 Query - Pagination Buttons not showing

Pagination buttons are not showing for some reason - any help would be greatly appreciated. Also, there is 8 posts showing unlike 10 as stated in the code.
<?php get_header(); ?>
<?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<div class="container-fluid p-0 pagewrap pagehead position-relative d-table" id="recent-projects">
<div class="container pageintro">
<div class="row">
<div class="col-12">
<h1>Recent Projects</h1>
<p>View our recent project case studies below</p>
</div>
</div>
</div>
</div>
<div class="container-fluid pt-5 pb-2">
<div class="container">
<div class="row">
<?php $catquery = new WP_Query( 'post_type=>projects&posts_per_page=10'.'&paged='.$paged );?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<div class="col-md-6">
<div class="grid">
<figure class="effect-oscar">
<?php the_post_thumbnail('large', array('style' => 'width: 100%; height: 200px; margin: 0px auto 15px auto;')) ?>
<figcaption>
<div class="m-auto">
<h2><span class="post-date"><?php echo get_the_date(); ?></span><?php the_title(); ?></h2>
<p><span><?php the_excerpt(); ?></span></p>
Read Post
</figcaption>
</figure>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<div class="pagination-nav text-center m-auto d-block p-4 mx-auto my-4">
<?php echo paginate_links( array(
'prev_text' => '<span>Previous Page</span>',
'next_text' => '<span>Next Page</span>'
)); ?>
</div>
</div>
<?php get_footer(); ?>
It is a custom post type page also
First of all your WP_Query args are incorrectly set. It should be:
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = [
'post_type' => 'projects',
'posts_per_page' => 10,
'paged' => $paged
];
$catquery = new WP_Query( $args );
?>
This is the preferred way to do it by most developers. However if you wonder what is your mistake... You set the post type with arrow notation but you must use the equal sign only.: post_type=projects
Next your pagination doesn't know about your custom post type. You must explicitly set it:
<?php
// Need a big base number:
$big = 999999999;
// Pagination
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' => '<span>Previous Page</span>',
'next_text' => '<span>Next Page</span>'
) );

How i can enable WordPress Posts Pagination

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.

issue with ajax pagination in bootstrap tab on wordpress

This is my code so i tried using jquery html load but nothing happens need help masters so any help from you guyz would be appreciated so it would be easy if you guyz would help me to have ajax pagination in my bootstrap navigation.
<?php
/*
* Template Name: Gallery
*/
get_header(); ?>
<script type="text/javascript" charset="utf-8">
</script>
<div class="about-content">
<div class="container">
<div class="border-bottom">
<!--images-->
<div class="services images">
<div class="container">
<div class="content">
<div class="images-tab" role="tabpanel">
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">VIEW ALL</li>
<?php $args = array('hide_empty' => false);
$image_category = get_terms('image_category',$args);
foreach($image_category as $image_categories ) :?>
<li role="presentation"><?php echo $image_categories->name; ?></li>
<?php endforeach;wp_reset_query(); ?>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<?php
$args = array('hide_empty' => false);
$image_category = get_terms('image_category',$args);
foreach($image_category as $image_categories ) :?>
<div role="tabpanel" class="tab-pane" id="<?php echo $image_categories->slug; ?>">
<div class="row" id="content">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$arg = array(
'post_type' => 'gallery',
'posts_per_page' =>1,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'image_category',
'field' => 'name',
'terms' => $image_categories
)
)
);
$query = new WP_Query($arg);
while($query->have_posts()) : $query->the_post();
$args1 = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
$category_object = wp_get_object_terms($post->ID,'image_category',$args1); ?>
<div id="content" class="post col-md-4"><?php the_post_thumbnail(); ?></div>
<?php endwhile; wp_reset_postdata();?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
<!--images-->
<div class="pagination-content">
<ul>
<li><i class="fa fa-play fa-rotate-180"></i></li>
<li> <?php
global $wp_query;
$big = 999999999; // need an unlikely integer
//$translated = __( 'Page', 'mytextdomain' ); // Supply translatable string
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_next' => false,
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages,
//'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
) );
?></li>
<li class="yellow"><i class="fa fa-play"></i></li>
</ul>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
There are lots of ajax wp pagination plugin you can use those https://www.designmaz.net/best-wordpress-pagination-plugins/

WP - How to call posts by page parent category

I'm setting up a blog listings template for a site with multiple parent pages, and I need this page template to be used across the site, various pages will require there own blog listings page. Calling relevant posts depending on the Parent category.
i.e:
Food (Site Homepage)
Food Blog (Listings page for all Food related posts)
Fruit (Parent page)
Fruit Blog (Listings page for all Fruit related posts)
Veg (Parent page)
Veg Blog (Listings page for all Veg related posts)
My problem is that the posts per correct parent category aren't being called. I'm getting all posts.
I've set my code up like this: Many thanks in advance.
<div id="bloglistings">
<?php wp_reset_query(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$current_cat = intval( get_query_var('cat') );
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : ?>
<div class="row">
<?php $count=0; ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="span6">
<div class="media feature one">
<a class="pull-left" href="<?php the_permalink(); ?>">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail', array('class' => 'img-circle')); ?>
<img class="hoverimage" src="<?php echo get_template_directory_uri(); ?>/img/icon-read-bloglistings.png" alt="">
</a>
<div class="media-body">
<h2><?php the_title(); ?></h2>
<p class="date"><?php the_time('F j, Y'); ?></p>
<p><?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
</div>
</div>
</div>
<?php $count++; ?>
<?php if ($count==2 ||$wp_query->found_posts==0) :
echo '</div><div class="row">';
?>
<?php $count=0; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<h2>Sorry but there are no posts.</h3>
<?php endif; ?>
<!-- PAGINATION -->
<div class="pagination">
<ul>
<li>
<?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' => $wp_query->max_num_pages,
) );
?>
</li>
</ul>
</div>
<?php wp_reset_query(); ?>
</div><!-- /.row -->
</div><!--/bloglistings-->
Try adding the category to the query.
Replace this:
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);
with
$args = array(
'post_type' => 'post',
'category__in' => array( $current_cat ),// category was missing here
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);

Categories