In my wordpress blog, i use wp paginate (default plugin) for pagination. I use a custom query to list the posts. Please see this link
Everything works fine. I show six posts per page (admin setting) and the number of pages are correct. But my problem is all the pages are shown in the pagination.
i.e. 1|2|3|4|5|6|7|8|9|10|>, but I want it like this |1|2|3|4|...|10|>.
What should I do, I'm new to wordpress. Can anyone help please?
Note:
No modifications done to wp paginate plugin.
Also changed the pagination settings in backend (wp-admin) to show 3 pages front and back of the current page and it doesn't work.
my index.php looks like this:
<?php
$paged = get_query_var('paged');
$args = array(
'post_type' => 'post',
'meta_key' => 'wpcf-fn',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged
);
$cust_query = new WP_Query( $args );
$temp = $wp_query;
$wp_query = $cust_query;
?>
<div> *** contents rendered on the page *** </div>
<div id="navigation" class="clearfix">
<?php if(function_exists('wp_paginate')) : wp_paginate(); ?>
</div>
<?php $wp_query = $temp;?>
<?php
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') ),
'total' => $wp_query->max_num_pages,
'show_all' => FALSE, //this will make paginate not to show all links.
'end_size' => 2, //will show 2 numbers on either the start and the end list edges.
'mid_size' => 0 //so that you won't have 1,2...,3,...,7,8
) );
?>
More details: https://developer.wordpress.org/reference/functions/paginate_links/
Related
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.
I have a website where On the home page I show all the posts of the website but now the requirement is to show all the posts excluding 3 Categories, but when I updated my WP_Query accordingly, Now Pagination started showing posts in loop, so Right now I am showing 12 posts in one page, so after 12 posts it repeats from the first post again and so one (right now my pagination is linked with infinite scroll but even if I disable infinite scroll, and switch to normal pagination, the issue still remains.
Below is my custom query
$fullquery = new WP_Query( 'cat=-12,-34,-56' );
while ($fullqury->have_posts()) : $fullqury->the_post();
And Below is my Pagination Code
<?php
$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' => $fullquery->max_num_pages
) );
?>
Try this
I'm not use the WP native pagination.
Actually I emptied the code in pagination.php
In your template.php, where you want to place your pagination
<div class="row">
<div class="col-md-12">
<div class="pagination">
<?php pagination_bar( $loop ); ?>
</div>
</div>
</div>
In the same template in the loop add :
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
And in your function.php (theme child)
// Pagination
function pagination_bar( $custom_query ) {
$total_pages = $custom_query->max_num_pages;
$big = 999999999;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $total_pages,
));
}
}
// END Pagination
It works for me
Ok I am lost , I am currently working on a underscore starter Theme with wordpress using PHP.
I use some custom post types. So in the homepage I display some custom posts with pagination using the loop
global $wp_query;
$wp_query = new WP_Query( array(
'post_type' => 'my_cpt',
'posts_per_page' => 8,
'paged' => $paged
)
);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post(); //display the post .. which I did
endwhile;
//Pagination starts here
$total_pages = $wp_query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
} //Pagination ends here
endif;
This code is in home.php , also index.php .
Aside from the main page (For the pages http://mywebsite/page/X ,where X is the page number and is >1 ) The website is directly dislaying 404.php , and everything works when I delete the 404.php from the theme !!
The Wordpress routing the user directly to 404.php if it exists, Am I missing something?
Is this supposed to work this way? , Link to hierarchy
The Urls that I was supposed to call were not http://mywebsite/page/X but :
http://mywebsite/my_cpt/page/X
My custom post type archive pages where redirecting to the 404. because I was missing this line in my custom post type registration function :
"has_archive" => true,
I had to add this line in my functions.php for it to work :
flush_rewrite_rules( false );
I added pagination to a custom post-type's archive using the paginate_links() function provided by Wordpress. The page numbers and paging itself work fine but posts start to re-appear at random whenever I go to another page.
Example:
Page 1
Product 1
Product 2
Product 3
Product 4
Product 5
Product 6
Page 2
Product 7
Product 4 (duplicate)
Product 8
Product 9
Product 2 (duplicate)
Product 10
And so on. No idea if this has any significance but it's always the same couple of posts that re-appear in the lists. Even after a hard refresh page 2 from the example would still look the same.
This is the custom query I used including the loop:
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
// CUSTOM QUERY ARGS
$product_args = array(
'post_type' => 'product',
'posts_per_page' => 9,
'paged' => $paged,
'orderby' => 'date',
'tax_query' => array(
'relation' => 'AND',
)
);
// CATEGORY FILTER
if(isset($_GET['cat']) && $_GET['cat']) {
$category = $_GET['cat'];
$product_args['tax_query'][] = array('taxonomy' => 'product-category', 'terms' => $category, 'field' => 'slug', 'operator' => 'IN');
}
$product_query = new WP_Query($product_args);
// THE LOOP
if ($product_query->have_posts()) {
while ($product_query->have_posts()): $product_query->the_post();
get_template_part('template-parts/content', 'product');
endwhile;
wp_reset_postdata();
} ?>
And the paginate_links() function as documented on Wordpress Codex:
<?php
$big = 999999999; // need an unlikely integer
$total_pages = $product_query->max_num_pages;
$translated = __( 'Page', 'project' ); // Supply translatable string
$current_page = max(1, get_query_var( 'paged' ));
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '<i class="far fa-angle-left"></i>',
'next_text' => '<i class="far fa-angle-right"></i>',
'before_page_number' => '<span class="screen-reader-text">'.$translated.' </span>'
)); ?>
I basically copied it from the documentation making only necessary changes to suit my setup.
I'll also add the rewrite rule I added to functions.php for the pagination links in case this could have anything to do with the issue:
/**
* Fix pagination on archive pages
*/
function pagination_rewrite() {
add_rewrite_rule(get_option('category_base') . '/page/?([0-9]{1,})/?$', 'index.php?pagename=' . get_option('category_base') . '&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
I read that custom queries can mess up pagination functions but they have a section specifically for handling Custom Queries in the documentation and I made sure to follow it to the letter. What could I be doing wrong here?
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 :)