phpwordpresswoocommercepagination

Woocommerce Pagination not working in custom template


Wordpress Version 6.2.2 Woocommerce Version 7.8.2 Permalinks set to /%postname%/ Product Permalinks set to /produkt/ Theme: TwentyTwentyOne

I've created a custom custom-template.php and added the woocommerce products.

I can see the first 22 products, but the pagination doesn't work, clicking on the pages always returns the first 22 products.

Clicking on a page (for example page 2) changes the url like this:

www.website.com/page/2/

Here is my code:

    <div id="content">
    <?php
    $paged = max(1, get_query_var('paged')); // Ensure the current page is at least 1

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 22,
        'paged' => $paged
    );

    $products = new WP_Query($args);

    if ($products->have_posts()) {
        echo '<div class="table"><div class="table-thead"><div class="table-tr"></div></div><div class="table-tbody">';
        while ($products->have_posts()) {
            $products->the_post();
            global $product;

            // Get the product data
            $product_name = $product->get_name();
            $product_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
            $product_permalink = get_permalink();

            // Output the product details
            echo '<div class="table-tr">';
            echo '<div class="table-td entryImage"><img src="' . $product_image[0] . '" alt="' . $product_name . '"></div>';
            echo '<div class="table-td entryName"><h2 class="c1"><a class="c1" href="' . $product_permalink . '">' . $product_name . '</a></h2></div>';

            // Additional product attributes
            $attributes = $product->get_attributes();
            if ($attributes && !empty($attributes)) { // Add a check for $attributes
                $counter = 1;
                foreach ($attributes as $attribute) {
                    $attribute_label = wc_attribute_label($attribute->get_name());
                    $attribute_value = array();

                    // Get attribute options as labels
                    $options = $attribute->get_terms();
                    foreach ($options as $option) {
                        $attribute_value[] = $option->name;
                    }

                    $css_class = 'entry' . $counter;

                    echo '<div class="table-td ' . $css_class . '"><span class="attribute-label c1">' . $attribute_label . ':</span> <span class="attribute-value">' . implode(', ', $attribute_value) . '</span></div>';

                    $counter++;
                }
            }

            echo '</div>';
        }
        echo '</div></div>';

        if ($products->max_num_pages > 1) {
            $pagination_args = array(
                'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
                'format' => '?paged=%#%',
                'current' => $paged,
                'total' => $products->max_num_pages,
                'prev_text' => '<< Zuruck',
                'next_text' => 'Weiter >>',
                'mid_size' => 5, // Number of links to display on each side of the current page
            );

            echo '<div class="pagination paginationtop">';
            echo paginate_links($pagination_args);
            echo '</div>';
        }
    } else {
        echo 'No products found.';
    }

    wp_reset_postdata();
    ?>
</div>

How can I make the pagination work? I would greatly appreciate any help.


Solution

  • Thank you Bhautik, your comment already helped me find the issue.

    I just had to change:

    $paged = max(1, get_query_var('paged')); // Ensure the current page is at least 1
    

    to

    $paged = max(1, (int) get_query_var('page')); // Ensure the current page is at least 1