I display child pages of a specific pages like this :
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'posts_per_page' => 3,
'order' => 'ASC',
'post_status' => 'publish',
'child_of' => $post->ID
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) { ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<div class="bloc-page">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
What I want is a numbered navigation, only three posts each pages. But I don't know how to do that and I tried a lot of solutions but nothing worked.
Thanks for your replies !
Just after the end of your while loop add following lines:
<?php
$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' => $parent->max_num_pages
) );
?>
For further help please read official wordpress pagination documentation here.
What kind of solutions did you do? Did you try wp_reset_postdata();?
Related
I have set up CPT with a custom taxonomy, yet I cannot get pagination working. First page renders fine but when I click through to "/page/2/", I get a 404 error.
I have a CPT named "species_guide" and a custom taxonomy named "species".
I attempted the solution outlined here: https://wpza.net/how-to-paginate-a-custom-post-type-in-wordpress/ and https://wpza.net/how-to-a-paginate-custom-taxonomy-archive-in-wordpress/ but to no avail.
Below is my code, any input would be appreciated:
in taxonomy-species.php I have the following:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$term_slug = get_query_var('species');
$args = array(
'post_type' => 'species_guide',
'tax_query' => array(
array(
'taxonomy' => 'species',//custom taxonomy name
'field' => 'slug',
'terms' => $term_slug
)
),
'posts_per_page' => 10,
'paged' => $paged
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'templates/post-archive' );
endwhile;
?>
<div class="pagination">
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'prev_text' => '«',
'next_text' => '»'
) );
wp_reset_postdata();
?>
and then in functions.php I have the following
function custom_tax_query_change( $query ) {
if ( ! is_admin() && $query->is_tax( 'species' ) ) {
$query->set( 'posts_per_page', 10 );
}
}
add_action( 'pre_get_posts', 'custom_tax_query_change' );
Should I be using the different species slugs in the is_tax() function? Not sure why I can't get this working
Your taxonomy template name should be taxonomy-species.php to match the taxonomy name, see WordPress documentation
You don't need to create a new instance of WP_Query() for the loop, simply copy existing taxonomy.php or archive.php file and name the file as described above. Make HTML/PHP changes as needed.
Here is the updated snippet for pagination:
<?php
// for total pages in the loop.
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,
'prev_text' => '«',
'next_text' => '»'
)
);
?>
i am completely customizing the downloads page, i use the following code on downloads page, it shows the page number correctly, but when i go to other pages, it gives the same results as the first page
path : /themes/theme-name-child/woocommerce/myaccount/downloads.php
<?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
$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' => $the_query->max_num_pages
) );
?>
i searched a lot, but i didn't find anything, i need your help
I am trying to add pagination for custom entries created through the CPT UI.
<?php $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; ?>
<?php
$query = new WP_Query( array(
'post_type' => array( 'feedbacks' ),
'posts_per_page' => 1,
'paged' => $paged,
));
?>
<?php if ($query->have_posts()) : ?>
<?php while ($query->have_posts()) : $query->the_post();?>
//post
<?php endwhile;?>
<?php endif; ?>
</div>
<?php
$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' => $query ->max_num_pages
));
?>
Pagination appears. But when you go to the second page gives 404.
We partially managed to solve the problem by adding the is archive attribute for the custom post type and changing the type of permalinks to default ones. In this case, everything works, but you need links with the page name
I read so many articles and didnt find answer.
my custom product loop in category:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
global $post;
$args = array(
'posts_per_page' => 6,
'product_cat' => $post->post_name, // **The father category**
'post_type' => 'product',
'paged' => $paged,
'page' => $page,
'pagination' => true
);
$loop = new WP_Query( $args );
echo $loop->request;
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>
////////////////// product info//////////////
<?php endwhile;
$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' => $loop->max_num_pages
) );
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
in category page i have same link:
http://localhost:3000/product-category/cats/videocard/
pagination link:
http://localhost:3000/product-category/cats/videocard/page/2/
but i have 404 on it.
my permalink settings:
enter image description here
my "Read" settings:
enter image description here
What i do wrong?
Possibly your problem is related to the WP_Query arguments. I have spotted the following issues:
the 'pagination' is not a valid argument,
the taxonomy argument ( 'product_cat' ) given as key is deprecated, you should use 'tax_query' instead.
the $page variable value is not defined ( at least not in your sample script ) and it seems that the definition of both 'page' and 'paged' arguments might cause conflicts to your query.
Try using the following:
global $post;
$args = array(
'posts_per_page' => 6,
'post_type' => 'product',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $post->post_name // !
),
),
);
$loop = new WP_Query( $args );
// echo $loop->request; - I guess this is a test left-over
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
/**
* No need to call the $product global within the loop,
* use the $loop->post or even better call the product object
* using the WooCommerce wc_get_product() function as displayed
* below
*/
// global $product;
$product = wc_get_product( $loop->post );
?>
////////////////// product info//////////////
<?php endwhile;
$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' => $loop->max_num_pages
) );
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
Last, but not least, the taxonomy term value you are giving for product_cat ( commented as the father category ) is the current post slug. I understand why you might want to do this, but under some circumstances, this could be also the reason you are getting these 404s. Therefore, if the above script is not solving your problem, I would advise you to look into this too.
When I use pagination for custom post type product its working fine but its not working for the categories of custom post type. for ex. pagination working for this http://localhost/wordpress/products/page/2/ and not for this http://localhost/wordpress/products/landscape/page/2/ its always showing the first page content. How to solve this? given below is my code.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'taxonomy' =>'product_cat','term' => $cat_name1,'orderby'=>'post_date','page'=>$paged );
$wp_query = new WP_Query($args);
if($wp_query->have_posts()) : while ($wp_query->have_posts()): $wp_query->the_post();
<div class="product_list">
<?php the_title();?>
</div>
<?php endwhile; ?>
<?php wp_pagenavi( array( 'query' => $wp_query ) );//plugin code ?>
<?php else : ?>
<!-- No posts found -->
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?php echo "No Products found for this categoy!." ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
Try this :
-Replace 'page' arguments with 'paged'
-Replace 'taxonomy' with 'tax_query'.
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$taxonomy_terms = get_terms( $taxonomy, array(
'hide_empty' => 0,
'fields' => 'ids'
) );
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $taxonomy_terms,
),
),'orderby'=>'post_date','paged'=>$paged );
In your question, you have used $cat_name1 for terms listing then please use following code:
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$taxonomy = 'product_cat';
$args = array( 'post_type' => 'product', 'posts_per_page' =>1,'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $cat_name1,
),
),'orderby'=>'post_date','paged'=>$paged );
Pagination :
please replace wp_pagenavi() function with following code:
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, $paged ),
'total' => $wp_query->max_num_pages
) );