Wordpress pagination static page - php

I'm working on a Wordpress template and want to use a static page as homepage. I made a pagination on that page, but when I go to another, the URL changes and also the posts of the other page shows up, but the pagination's current page is the same. It says that it's still on 'Page 1 of ....' in the pagination.
Does someone know how I can change the current page?
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $page,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<span class='page-numbers page-num'>Page " . $page . " of " . $numpages . "</span> ";
echo $paginate_links;
}
}
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged
);
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
// Get all posts
get_template_part( 'content', 'post' );
endwhile; ?></div>
<!-- end of the loop -->
<nav class='paging-navigation'>
<?php
if (function_exists(custom_pagination)) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
?></nav>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?></div>

Solved the problem! Put this:
<?php
if (function_exists(custom_pagination)) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
After the wp_reset_postdata, also removed 'page' => $paged from $query_args, this worked for me. :)

Related

Numeric pagination not appearing inside custom loop - Wordpress

I have been trying to add numeric pagination to my posts page which are filtered by category. The problem is that pagination won't appear when I call it. Pagination works perfectly fine when I use previous_posts_link and next_posts_link but this is not excatly what I want to achieve. What am I missing here?
My filtered category page.
<?php
// Get post ID
$post_type = get_post_type( $post->ID );
// Get category ID
$category_id = get_cat_ID(single_cat_title('', false));
// Wordpress pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// WP_Query arguments
$args_news = array (
'post_type' => array( 'post' ),
'pagination' => true,
'posts_per_page' => '2',
'orderby' => 'date',
'paged' => $paged,
'cat' => $category_id,
);
// The Query
$query = new WP_Query( $args_news );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<?php get_template_part('categorytwo',get_post_format()); ?>
<?php }
} else {
// no news found
}
?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php
// Reset postdata
wp_reset_postdata();
?>
This pagination function works fine on my index.php page when I call it out.
<?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_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 4
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
Functions.php
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
global $wp_query;
$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' => $wp_query->max_num_pages
) );
}
endif;
When I call it out on my index.php page it works fine.
<?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_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 4
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile; ?>
<div class="pagination">
<?php my_pagination(); ?>
</div>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
Use this code on your loop:
<div class="posts_blog">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post', // Your post type name
'posts_per_page' => 5,
'paged' => $paged,
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
?>
<div class="posts_container ">
// Posts format: <?php get_template_part('catalog',get_post_format()); ?>
</div>
<?php
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1) {
?>
<div class="pagination">
<?php
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%'.'/#posts-blog',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('<'),
'next_text' => __('>'),
));
?>
</div>
<?php
}
}
wp_reset_postdata();
if (function_exists("pagination")) {
pagination($wp_query->max_num_pages);
}
?>
</div>

pagination doesn't work in search page wordpress

halo guys, my pagination can't work under search page. i made custom search page and the link will be like this
/search/?category=2&tag=all
when i click next link. the link become this
/search/page/2/?category=2&tag=all
but post didn't change. when i var_dump get_query_var('paged'); it always return 0. how do i can resolve this.
here is my code in display post
<?php
$CurrentPage = get_query_var('paged');
$args = [
'posts_per_page' => 2,
'paged' => $CurrentPage,
'cat' => $search_category,
'tag' => $search_tag,
];
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post();
$image = get_field('thumbnail');
?>
here is for display pagination
<?php previous_posts_link('<img src="'.get_template_directory_uri().'/static/images/column/arrow01.svg" alt="prev">PREV', $loop->max_num_pages) ?>
<?php list_pagination($loop); ?>
<?php next_posts_link('NEXT<img src="'.get_template_directory_uri().'/static/images/column/arrow02.svg" alt="next">', $loop->max_num_pages); ?>
here is my code in functions.php
function list_pagination($loop) {
$big = 999999999;
$paged = paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'prev_next' => false,
'type' => 'array',
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => '',
'total' => $loop->max_num_pages
));
if ( ! empty( $paged ) ) :
echo '<ul class="pager_list">';
$no = 1;
foreach ( $paged as $key => $page_link ) :
echo '<li class="pager_item">'.$page_link .'</li>';
endforeach;
echo '</ul>';
endif;
}
anywrong with my code?
$CurrentPage = get_query_var('paged'); should be changed to $CurrentPage = get_query_var('page'); i.e. without d at the end of paged.

Wordpress Custom Post type Category View with Pagination

I have a custom post type 'charters' and within my category.php page I have a loop pulling in 'charter' posts for the current category id. I have a custom pagination setup displaying page numbers when I click page 2 link, it goes to URL /page/2 which displays a 404 error. I can't figure out why I am getting the 404. If I adjust my posts per page the pagination updates and displays the correct number of pages but the links do not show up.
Category.php Code:
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$product_args = array(
'post_type' => 'charters',
'posts_per_page' => 10, //the same as the parse_query filter in our functions.php file
'paged' => $paged,
'page' => $paged,
'cat' => $cat
);
$product_query = new WP_Query( $product_args ); ?>
<?php if ( $product_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?>
<article class="loop">
<h3><?php the_title(); ?></h3>
<div class="content">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists( 'custom_pagination' )) :
custom_pagination( $product_query->max_num_pages,"",$paged );
endif;
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Functions.php Code:
function prefix_change_cpt_archive_per_page( $query ) {
//* for cpt or any post type main archive
if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'product' ) ) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' );
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' );
function custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
My Permalink Settings are set to "Post Name" I have a feeling this has something to do with rewrites but I can not figure out how to get the page urls to work correctly. An example of my categories are as follows:
/category/long-beach/
/category/san-diego/
you should try this:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'charters',
'cat'=> $cat,
'post_per_page'=> 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
wp_reset_query();
?>
<?php echo pnavigation( $query ); ?>
then you need to add following code to function.php :
function pnavigation( $wp_query ) {
$big = 999999999; // need an unlikely integer
$pages = 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,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => '←',
'next_text' => '→',
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination pagination-lg">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
}
}
you need to just replace url (client-testimonials) with your url
function pagination_rewrite() {
add_rewrite_rule('client-testimonials/page/?([0-9]{1,})/?$', 'index.php?pagename=client-testimonials&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
If you are using default pagination then please use below code.
<?php
$current_page = get_queried_object();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'charters',
'paged' => $paged,
'cat'=> $cat);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
next_posts_link( 'Older Entries', $my_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
wp_reset_query();
?>
Add below code in your function.php
function posts_on_categorypage( $query ) {
if ( $query->is_category()) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'posts_on_categorypage' );
There was a couple of errors in my code. This is what I get for copy/pasting code and not going through it line by line. Thanks everyone who helped out, but I got this to work by changing:
Bad Code:
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
Good Code:
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( $cat ) ) {
$query->set( 'post_type', array( 'charters' ) );
$query->set( 'posts_per_page', '10' );
}
}

Custom Post Pagination Not Working

I know there have been so many pagination questions, but I'm really stumped here. I have a "gallery" hierarchical custom post type. For the parent posts, I'm using an index template, and the child posts uses more typical single page code.
Here's some of the single-gallery.php file. I just check to see if the post has children. If it does, I want it to be an index page.
get_header();
$template = get_template_directory_uri();
//If it's an index page, redirect it
$args = array(
'post_parent' => $post->ID,
'post_type' => 'gallery',
'numberposts' => -1,
'post_status' => 'publish'
);
$children = get_children( $args );
if ($children){
get_template_part('template-parts/content', 'gallery-index');
get_footer();
} else{
After that else, it outputs all the code for the single display, which is working fine.
In content-gallery-index.php, I have.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
Then it goes on to handle code for another, unrelated page. Lower in the template, I have the following (I included all of it by request):
<?php elseif (is_page(343) || $post_type == 'gallery' ):
$post_parent = 343;
$type = 'page';
if ($post_type == 'gallery'){
$post_parent = $post->ID;
$type = 'gallery';
}
$args = array(
'post_type' => $type,
'post_parent' => $post_parent,
'posts_per_page' => 6,
'paged' => $paged,
);
$query = new WP_Query($args);
if ($query->have_posts()): ?>
<div class="box-steps">
<?php while($query->have_posts()): $query->the_post();
$image = get_the_post_thumbnail();
$permalink = get_the_permalink();
if (!get_field('dont_include_in_services')): ?>
<section class="box-step">
<div class="box-step-img-wrapper">
<a href="<?php echo $permalink; ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<div class="box-step-content-wrapper">
<h2 class="box-step-title our-services-title header-font"><?php echo get_the_title(); ?></h2>
<?php if ($post_type == 'gallery'): ?>
<p class="box-step-content"><?php echo get_the_content($post->ID); ?></p>
<?php else: ?>
<p class="box-step-content"><?php echo esc_html(get_field('preview_text')); ?></p>
<a class="box-step-link block bold" href="<?php echo $permalink; ?>">
READ MORE
</a>
<?php endif; ?>
</div>
</section>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_postdata(); ?>
<!-- pagination here -->
<?php
custom_pagination($query->max_num_pages,"",$paged);
?>
<?php endif; ?>
functions.php
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$prev_arrow = is_rtl() ? '»' : '«';
$next_arrow = is_rtl() ? '«' : '»';
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'list',
'add_args' => false,
'add_fragment' => '',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
//echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}
The strange thing is that the numbered pagination is output fine, but when I click on page two, it's like the URL just gets rewritten and the page just refreshes. This same code has worked on another page, but that page is not a custom post type. I've also tried the trick of turning wp_query to null and resetting it, but then I get other errors.

Wordpress pagination - Can't go to other pages

I'm using wordpress and I'm trying to create a pagination with wordpress WP_query. I have used a tutorial for this and some people had the same problem like me that they couldn't connect to the second or third page of their pagination. I have 11 posts in my database and I have split them so they show 4 per page and whether I try to go to second or third page, it just says "404 page not found". Here is my index.php:
<?php
get_header();?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$query_args = array(
'post_type' => 'all_products',
'posts_per_page' => 4,
'paged' => $paged,
'page' => $paged
);
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<article class="loop">
<div class="product">
<h1><?php the_title(); ?></h1>
<ul>
<li><strong>Description:</strong> <?php echo(types_render_field( "description_of_the_product", array( 'raw' => true) )); ?></li>
<li><strong>Type:</strong> <?php echo(types_render_field( "type", array( 'raw' => true) )); ?></li>
<li><strong>Price:</strong> <?php echo(types_render_field( "price", array( 'raw' => true) )); ?></li>
<li><strong>Manufacturer:</strong> <?php echo(types_render_field( "manufacturer", array( 'raw' => true) )); ?></li>
<li><strong>Availability:</strong> <?php echo(types_render_field( "availability:", array( 'raw' => true) )); ?></li>
<li><strong>Reference:</strong> <?php echo(types_render_field( "reference", array( 'raw' => true) )); ?></li>
Read more
<ul>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists('custom_pagination')) {
custom_pagination($the_query->max_num_pages,"",$paged);
}
?>
<?php next_posts_link( $label , $max_pages ); ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php get_footer(); ?>
Any help is greatly appreciated.
Pagination function(custom_pagination):
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default queries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}

Categories