我有这样的代码用于在woocommerce中获取和显示产品:
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 30
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
}
问题是,代码一次显示30个产品,它不会添加页面链接,因此可以查看其他产品,有没有办法做到这一点,还是我错过了什么?
答案 0 :(得分:1)
<ul class="products">
<?php
global $paged;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
<?php wp_reset_postdata(); ?>
</ul><!--/.products-->
答案 1 :(得分:1)
您可以查看下面的代码吗?我希望这段代码对你有用。
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'product',
'posts_per_page' => 30,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
endif;
wp_reset_postdata();