Jak wyświetlić paginację dla produktów WooCommerce
![Jak wyświetlić paginację dla produktów WooCommerce](https://oen.pl/wp-content/uploads/2023/01/apple-touch-icon@2.png)
Napisałem ten kod, aby wyświetlić produkty WooCommerce
<div class="products-container container mb-4">
<div class="products-filter-section mt-3">
<?php do_action('woocommerce_before_shop_loop'); ?>
</div>
<?php
while (have_posts()) :
the_post();
global $product;
?>
<div class="product card shadow-lg mb-4">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo get_the_post_thumbnail_url(); ?>" class="card-img-top image border-0 p-0" alt="...">
</a>
<div class="body card-body">
<h3 class="h6 mb-1 title"><a class="fw-bold" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php
if(get_field('hide_product_author_name', 'options') != '1') {
?>
<div class="author">By <a href="#"><?php echo get_the_author(); ?></a></div>
<?php
}
?>
</div>
<div class="footer card-footer border-0 pt-0 bg-white d-flex flex-wrap">
<div class="info align-items-center flex-grow-1">
<div class="price">
<?php if ($product->get_sale_price()) : ?>
<?php
if(get_field('hide_product_author_name', 'options') != '1') {
?>
<span class="original text-decoration-line-through fw-bold me-1"><?php echo wc_price($product->get_regular_price()); ?></span>
<?php
}
?>
<span class="with-off fw-bold"><?php echo wc_price($product->get_sale_price()); ?></span>
<?php else : ?>
<span class="original fw-bold"><?php echo wc_price($product->get_regular_price()); ?></span>
<?php endif; ?>
</div>
<div class="d-flex rate">
<?php
for ($i = 0; $i < 5; $i++) {
if ($i < round($product->get_average_rating())) {
echo '<span class="mdi mdi-18px mdi-star"></span>';
} else {
echo '<span class="mdi mdi-18px mdi-star text-secondary"></span>';
}
}
?>
<?php
if(get_field('hide_product_rate', 'options') != '1') {
?>
(<?php echo $product->get_rating_count(); ?>)
<?php
}
?>
</div>
<?php
if(get_field('hide_product_sale_count', 'options') != '1') {
?>
<div class="sale"><?php echo $product->get_total_sales(); ?> Sales</div>
<?php
}
?>
</div>
<div class="align-items-end d-flex gap-2 action ms-auto">
<?php
if(get_field('preview_link')) {
?>
<a href="<?php echo esc_url( get_field('preview_link') ); ?>" class="btn rounded px-3 preview" target="_blank">Preview</a>
<?php
}
?>
<?php
if(get_field('hide_product_add_to_cart', 'options') != '1') {
?>
<?php
$product_id = $product->get_id();
$add_to_cart_url = wc_get_checkout_url() . '?add-to-cart=" . $product_id;
?>
<a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="btn rounded add-to-cart">
<span class="mdi mdi-cart-plus mdi-18px"></span>
</a>
<?php
}
?>
</div>
</div>
</div>
<?php endwhile; ?>
Napisałem również ten kod do paginacji moich produktów
function custom_pagination()
{
global $wp_query;
$big = 999999999; // need an unlikely integer
$pagination = 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_next' => true,
'prev_text' => '<span class="mdi mdi-18px mdi-chevron-left"></span>',
'next_text' => '<span class="mdi mdi-18px mdi-chevron-right"></span>',
'type' => 'array',
'mid_size' => 3,
'end_size' => 1
));
if ($wp_query->max_num_pages > 1) :
?>
<ul class="pagination justify-content-center">
<?php foreach ($pagination as $page_link) : ?>
<li class="page-item <?php echo strpos($page_link, 'current') ? 'active' : ''; ?>">
<?php echo str_replace('page-numbers', 'page-link', $page_link); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif;
}
Mam 10 produktów, ale kiedy ustawię liczbę postów na 2 w ustawieniach WordPressa i chcę, aby moje produkty wyświetlały się na 5 stronach, nic się nie dzieje i nie wyświetla się paginacja. Dziękuję za pomoc i powiedz mi, co jest nie tak z moim kodem