在wordpress主页外创建1个分页



[Wordpress]我的主页是在一个名为template-home.php的模板中编写的

我按1个类别查询了1个产品(woocommerce)列表。我想在它下面显示一个分页,怎么做呢?

链接文件template-home.php: https://prnt.sc/1t71zil网站:https://holavietnam.vn/

查询:

<?php
$args = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => 152,
)
)
));
if ($args->have_posts()) :
while ($args->have_posts()) : $args->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-6 pad_des">
<div class="item_product_s wow flipInX">
<a href="<?php the_permalink() ?>">
<div class="name_product_s">
<h3>
<?php the_title() ?>
</h3>
<p class=" price">
<del aria-hidden="true">
<span class="woocommerce-Price-amount amount">
<bdi><?php echo get_post_meta(get_the_ID(), '_regular_price', true); ?><span class="woocommerce-Price-currencySymbol">₫</span></bdi>
</span>
</del>
<ins>
<span class="woocommerce-Price-amount amount">
<bdi><?php echo get_post_meta(get_the_ID(), '_sale_price', true); ?><span class="woocommerce-Price-currencySymbol">₫</span></bdi>
</span>
</ins>
</p>
</div>
</a>
<a href="<?php the_permalink() ?>">
<div class="zoom_product_s">
<?php the_post_thumbnail(); ?>
</div>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
wp_reset_query();
endif;
?>

首先,我们应该添加一个查询变量,默认WordPress支持paged,但我不确定你的主页允许它。试试吧,如果不行就告诉我。

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

然后在while后面加上the_posts_pagination();

我更新了你的代码,我还没有测试它。试着添加它来检查结果。

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = new WP_Query(array(
'posts_per_page' => 10,  // your product items per page here
'paged'          => $paged,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => 152,
)
)
));
if ($args->have_posts()) :
while ($args->have_posts()) : $args->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-6 pad_des">
<div class="item_product_s wow flipInX">
<a href="<?php the_permalink() ?>">
<div class="name_product_s">
<h3>
<?php the_title() ?>
</h3>
<p class=" price">
<del aria-hidden="true">
<span class="woocommerce-Price-amount amount">
<bdi><?php echo get_post_meta(get_the_ID(), '_regular_price', true); ?><span class="woocommerce-Price-currencySymbol">₫</span></bdi>
</span>
</del>
<ins>
<span class="woocommerce-Price-amount amount">
<bdi><?php echo get_post_meta(get_the_ID(), '_sale_price', true); ?><span class="woocommerce-Price-currencySymbol">₫</span></bdi>
</span>
</ins>
</p>
</div>
</a>
<a href="<?php the_permalink() ?>">
<div class="zoom_product_s">
<?php the_post_thumbnail(); ?>
</div>
</a>
</div>
</div>
<?php
endwhile;
the_posts_pagination();
wp_reset_postdata();
wp_reset_query();
endif;
?>

最新更新