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.
Related
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();
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>
I am having an issue with the pagination on my WordPress blog page. The next and previous page buttons take me to a new page but all pages show the same 10 blog posts.
Here is the code I am using:
<?php
$args = array('cat' => '-27');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = '?&paged=%#%';
} else {
$format = 'page/%#%/';
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
?>
Any help would be greatly appreciated!
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. :)
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.