How to fix wordpress pagination redirect? - php

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.

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();

Pagination not working while using custom query

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

Wordpress numbered Pagination & updating the current page using query_posts

I can add numbered pagination to wordpress fine, the problem is when I click on page two the link for page one doesn't appear to be clickable. The loop still thinks it's on page one.
This is my current code.
<?php query_posts('posts_per_page=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
....
<?php endwhile;
endif; ?>
<?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
) );
?>

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 :)

wordpress Static Page pagination

Im Using a post query in wordpress BUT the Pagination is not working,
i don't know whats the problem BUT here is my code and i guess it's correct and no problem with it
it shows that there is pages BUT when i Click on Next Page it refresh the page and don't show any new results just the same page.
Im Using it on Static page to be The Home page of my theme
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_query = query_posts(array(
'post_type' => 'cover', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 1
));
?>
<?php if ( have_posts() ) : ?>
<?php
while ( have_posts() ) : the_post();
?>
<?php endwhile; ?>
///Pagination Function in Functions.php
<?php my_pagination(); ?>
<?php else: ?>
No Results
<?php endif; ?>
Pagination Function
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') ),
'total' => $wp_query->max_num_pages
) );
}
endif;
Wordpress Static front page Pagination after a lot of Searches and googling i fix it by using
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array('post_type'=>'cover','posts_per_page'=>2,'paged'=>$paged);
query_posts($args);
I've found a small error in Youssef Subehis solution. It is missing a "d" in "paged" here get_query_var('page'). This fixed the wordpress page pagination problem for me:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page'=>10,'paged'=>$paged);
query_posts($args);
This is referecend in the official wordpress documentation here.

Categories