Hi,
I need help. Me wrote query in my wordpress theme to fetch latest products. Everything is working perfect. But don't know why pagination not working. I did select 9 post in backend. it's appear 9 on my home page. But i want to show all products on homepage with pagination.

Here is my homepage url :

But if you see in category page pagination working fine.

Here is my homepage code.

Pliz let me know where is the problem:

<div class="products">
                <?php

// Setup your custom query
$args = array( 'post_type' => 'product', );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="pro-item">

<div class="pro-item-title"><a>post->ID ) ?>"><?php the_title(); ?></a></div>
<div class="pro-item-content">

<div class="pro-item-image">    <a>post->ID ) ?>">
        <?php
        if(has_post_thumbnail()) {
                    the_post_thumbnail($size);
                }
            ?>
            <?php
                global $more;
                $more = 0;
            ?>
    </a></div><div class="pro-item-desc"><?php the_content(); ?>Price : $<?php echo get_post_meta($post->ID, "_regular_price", $single = true); ?></div><div class="pro-item-btn">
    <?php

do_action( 'woocommerce_after_shop_loop_item' ); ?>
    </div>

</div></div>
<?php endwhile; wp_pagenavi(); // Remember to reset ?>

            </div>

Can anyone recode and replace his code again. I really need this.

Dani AI

Generated

you are almost there. Pagination is breaking because the custom WP_Query on the homepage never gets a paged value, and PageNavi is reading the global $wp_query instead of your custom query. On a static front page WordPress may use the query var page, so you need to account for both and then hand that query to wp_pagenavi. @LastMitch is right that Woo support can help, but this bit is pure WordPress loop setup.

<?php
$paged = max( 1, (int) get_query_var('paged'), (int) get_query_var('page') );

$products = new WP_Query( array(
  'post_type'           => 'product',
  'post_status'         => 'publish',
  'posts_per_page'      => 9,          // or get_option('posts_per_page')
  'paged'               => $paged,
  'ignore_sticky_posts' => true,
) );

if ( $products->have_posts() ) :
  echo '<div class="products">';
  while ( $products->have_posts() ) : $products->the_post();
    // your markup here, or:
    // wc_get_template_part( 'content', 'product' );
  endwhile;
  echo '</div>';

  if ( function_exists( 'wp_pagenavi' ) ) {
    wp_pagenavi( array( 'query' => $products ) );
  } else {
    the_posts_pagination();
  }
  wp_reset_postdata();
endif;
?>

A few quick checks:

  • Do not use query_posts for this; stick with WP_Query and call wp_reset_postdata() after the loop.
  • If you previously set no_found_rows => true, remove it. Pagination needs total row counts.
  • If you prefer, you can avoid a custom loop and use pre_get_posts to make the main homepage query return products; then wp_pagenavi() works without passing a custom query.
Member Avatar for Member #949455

But don't know why pagination not working. I did select 9 post in backend. it's appear 9 on my home page. But i want to show all products on homepage with pagination.

It's an plugin. If you still have questions post it here:

http://wordpress.org/support/plugin/woocommerce

The reason is very simple it's their product and platform. Someone over there will be more familiar with this.

I hope you understand that.

or you can look at this:

http://wpsnacks.com/wordpress-tutorials/how-to-add-custom-pagination-page-navigation-to-woocommerce/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.