Strange pagination behavior giving white page - php

I' am using WordPress pagination. I have used this code on my other pages and it's working fine in there, but on this page "schedules" when I try to goto next paged link it gives white page.
I have reset Permalink Settings because thats the only solution i found on the Internet. Still nothing same problem.
In the database their are total of 9 records under this custom post type so by pagination it will be 3 per page.
Pagination Code
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'schedules',
'posts_per_page' => 3,
'post_status' => 'publish',
'paged' => $paged
);
query_posts($args);
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$schedules_vessel = get_field('schedules_vessel');
$schedules_voyage = get_field('schedules_voyage');
$schedules_port = get_field('schedules_port');
$schedules_country = get_field('schedules_country');
$schedules_arrival = get_field('schedules_arrival');
$schedules_departure = get_field('schedules_departure');
$date = DateTime::createFromFormat('d/m/Y h:i A', $schedules_arrival);
$schedules_arrival_date = $date->format('d M Y');
$schedules_arrival_time = $date->format('G:i');
$date = DateTime::createFromFormat('d/m/Y h:i A', $schedules_departure);
$schedules_departure_date = $date->format('d M Y');
$schedules_departure_time = $date->format('G:i');
?>
<?php endwhile; endif; ?>
<?php require_once( get_template_directory() . "/snippets/pagination.php"); ?>
In pagination.php
<?php
global $wp_query;
$temp = "";
$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,
'prev_text' => 'Previous',
'next_text' => 'Next',
'type' => 'list'
));
?>
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>

I hate answering my own question but yes I found the solution. The problem was with the custom post type slug "schedules" was had the same slug as the page name "schedules" I think that was conflicting, well I don't think it should but anyways.

Related

How To Make Pagination For Custom Post Type In Wordpress?

PRECONDITION:
I created post type 'comparison'.
I created archive page for 'comparison' post type only.
TASK: I need to create pagination at archive page of 'comparison'.
PROBLEM: I tried to use
<?php echo paginate_links(); ?>
but it doesn't work, pls help.
Try the following code
$query = new WP_Query(
array(
'posts_per_page'=>30,
'post_type'=>'comparison',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
)
);
?>
<?php while ($query -> have_posts()) : $query -> the_post(); ?>
//your code
endwhile;
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$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' => $query->max_num_pages
) );
}
wp_reset_postdata();

How to fix wordpress pagination redirect?

I am using pagination in wordpress category.php page. everthing is fine but when I click to next page i got following error
Template is missing. Standalone themes need to have a index.php template file. Child themes need to have a Template header in the style.css stylesheet.
My url is some thing like
https://test.com/category/cat_name/page/2/
Here is my code:
$cat_name = $category->name;
$cat_id = get_cat_ID( $cat_name );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=1&post_type=post&paged='.$paged.'&cat='.$cat_id);
while ($wp_query->have_posts()) : $wp_query->the_post();
/// loop here
<?php endwhile; ?>
Code for pagination:
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
echo '<div class="custom-pagination">';
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'prev_text' => __('Previous'),
'next_text' => __('Next'),
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
echo '</div>';
?>
Should I add some code in htaccess file or function.php for redirect in category.php?
I got the answer by my self. I need to change the Blog pages show at most in reading section of Dashboard. I set same number of showposts i.e 1 in Blog pages show at most.

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.

adding pagination to a custom post type in wordpress

I am facing a little issue while adding pagination to a custom post type which I've created inside Wordpress. The pagination links are appearing on the template where I've all the posts, inside the custom post type, listed but when I click on the link to view Older posts, it opens the second page but the same posts from the first page are displayed there. Moreover, on the second page, the 'Older posts' link doesn't update to '../page/3', instead it stays '../page/2'. I followed the steps specified here (https://stackoverflow.com/a/18325002/2115001) and modified my code according to the information listed under 'Option 2'. Here's what my code currently looks like:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=3&post_type=medals'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
// loops code here
endwhile;
echo '<nav>';
echo previous_posts_link('« Newer');
echo next_posts_link('Older »');
echo '</nav>';
$wp_query = null;
$wp_query = $temp; // Reset
?>
Do I need to add some code to the functions.php file in order for this to function properly or there's something wrong in my original code? Thanks.
Try below code
$paged = get_query_var('page') ? get_query_var('page') : 1;
$args = array('posts_per_page' => 3,
'paged'=> $paged,
'post_type' => 'medals');
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
// loops code here
endwhile;
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,
'next_text' => __('Next »'),
));
Try this:
<?php
$type = 'portfolio';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
'caller_get_posts'=> 1
);
$temp = $wp_query; // assign original query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query();
wp_query->query($args);
?>
then put your permalinks to the default structure and back to how the structure was.
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article class="">
<div class="">
<h4><?php the_time( 'm' ); ?></h4>
<h4><?php the_time( 'd' ); ?></h4>
</div>
<div class="">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</a>
</article>
<?php endwhile; // End the loop. Whew. ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
//echo esc_url( get_pagenum_link());
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages,
'type'=>'list',
'prev_text' => __('<'),
'next_text' => __('>'),
) );
?>
Hope it will work for you.

wordpress - pagination in subpage

I have a child page with custom post query and i need to paginate them. The pagination query itself works, however, the links do not. Currently my page link is like this - /parent-page/child-page/ and the page links goes to /parent-page/child-page/page/2, which returns 404. How can I make this work in this case?
The page link function:
function my_pagination() {
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
) );
}
and a simple custom query in main page
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'press_gallery',
'paged' => $paged,
'posts_per_page' => 30
));
Just follow these steps and you will be done-
1)Install WP-PageNavi plugin and activate it.
2)Now your code should be like this-
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'press_gallery',
'paged' => $paged,
'posts_per_page' => 30
));
$loop = new WP_Query($mypost);
while ($loop->have_posts()) : $loop->the_post();?>
<!--your code here-->
<?php endwhile; wp_reset_query(); ?>
<!--at the end just call this page navi function and you are done-->
<?php wp_pagenavi(array('query' => $loop)); ?>
3)You are done :)

Categories