wordpress Woocommerce分页不适用于自定义模板

rjjhvcjd  于 2023-08-03  发布在  WordPress
关注(0)|答案(1)|浏览(127)

Wordpress Version 6.2.2Woocommerce Version 7.8.2Permalinks set to /%postname%/Product Permalinks set to /produkt/Theme: TwentyTwentyOne
我创建了一个定制的custom-template.php,并添加了woocommerce产品。
我可以看到前22个产品,但分页不起作用,点击页面总是返回前22个产品。
点击一个页面(例如第2页)会改变URL如下:
www.website.com/page/2/
下面是我的代码:

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

字符串
如何才能使分页工作?我将非常感谢任何帮助。

w6lpcovy

w6lpcovy1#

谢谢Bhautik,你的评论已经帮助我找到了问题。
我只需要改变:

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

字符串

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

相关问题