我在我的网站上使用高级自定义字段插件来展示产品。在显示 8 个产品后,我需要对 Repeater Field 循环实现分页。我在这里找到了解决方案 -> http://jonathannicol.com/blog/2014/03/06/paginating-an-advanced-custom-fields-repeater/ 这很有效,但不在主页上。我在 mydomain.com/test 上进行了测试,第二页显示在 mydomain.com/test/2 上,但在主页网站 mydomain.com/2 上显示 404 错误。
我的代码位于 template-home.php
<div class="container">
<div class="row">
<?php
$promocja = pll__('PROMOCJA!');
$zobacz = pll__('Zobacz produkt'); ?>
<div class="container productLoopContainer">
<div class="row">
<?php
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$row = 0;
$cols_per_page = 8; // How many images to display on each page
$cols = get_field( 'produkt' );
$total = count( $cols );
$pages = ceil( $total / $cols_per_page );
$min = ( ( $page * $cols_per_page ) - $cols_per_page ) + 1;
$max = ( $min + $cols_per_page ) - 1;
if( have_rows('produkt') ) :
while ( have_rows('produkt') ) : the_row();
$row++;
// Ignore this image if $row is lower than $min
if($row < $min) { continue; }
// Stop loop completely if $row is higher than $max
if($row > $max) { break; }
$kategoria = get_sub_field('kategoria');
$urlProduktu = get_sub_field('adres_url');
$zdjecie = get_sub_field('zdjecie');
$nazwa = get_sub_field('nazwa_produktu'); ?>
<div class="col-sm-6 col-md-3">
<div class="product clearfix product-hover" id="<?php echo $kategoria; ?>">
<div class="left">
<div class="sale-badge"><?php echo $promocja; ?></div>
<div class="image">
<a href="<?php echo $urlProduktu; ?>"><img src="<?php echo $zdjecie; ?>" class="zoom-image-effect wp-post-image" alt="<?php echo $nazwa; ?>"></a>
</div>
</div>
<div class="right">
<div class="name"><a href="<?php echo $urlProduktu; ?>"><?php echo $nazwa; ?></a></div>
<div class="typo-actions clearfix">
<div class="addtocart">
<a href="<?php echo $urlProduktu; ?>" class="button cart-links add_to_cart_button ajax_add_to_cart product_type_variable"><?php echo $zobacz; ?></a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;
// Pagination
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'total' => $pages
) ); ?>
<?php else:
// no rows found
endif; ?>
</div>
</div>
</div>
您知道如何解决这个问题吗?我尝试在functions.php中添加rewrite_endpoint但没有成功。
感谢您的帮助!
更新: 我向functions.php添加了代码,并稍微更改了我的模板代码,现在没有404了。第二页显示在mydomain.com/products/2,这很棒,但没有产品 - 没有显示产品,只有单词“档案”。
我的模板:
<div class="container">
<div class="row">
<?php
$promocja = pll__('PROMOCJA!');
$zobacz = pll__('Zobacz produkt'); ?>
<div class="container productLoopContainer">
<div class="row">
<?php
// Get query var
global $wp_rewrite;
if ($wp_rewrite->using_permalinks()) {
if( !empty( get_query_var('products'))) {
$page = get_query_var('products');
} else {
$page = 1;
}
} else {
if( !empty( $_GET['products'] )) {
$page = $_GET['products'];
} else {
$page = 1;
}
}
$row = 0;
$cols_per_page = 8; // How many images to display on each page
$cols = get_field( 'produkt' );
$total = count( $cols );
$pages = ceil( $total / $cols_per_page );
$min = ( ( $page * $cols_per_page ) - $cols_per_page ) + 1;
$max = ( $min + $cols_per_page ) - 1;
if( have_rows('produkt') ) :
while ( have_rows('produkt') ) : the_row();
$row++;
// Ignore this image if $row is lower than $min
if($row < $min) { continue; }
// Stop loop completely if $row is higher than $max
if($row > $max) { break; }
$kategoria = get_sub_field('kategoria');
$urlProduktu = get_sub_field('adres_url');
$zdjecie = get_sub_field('zdjecie');
$nazwa = get_sub_field('nazwa_produktu'); ?>
<div class="col-sm-6 col-md-3">
<div class="product clearfix product-hover" id="<?php echo $kategoria; ?>">
<div class="left">
<div class="sale-badge"><?php echo $promocja; ?></div>
<div class="image">
<a href="<?php echo $urlProduktu; ?>"><img src="<?php echo $zdjecie; ?>" class="zoom-image-effect wp-post-image" alt="<?php echo $nazwa; ?>"></a>
</div>
</div>
<div class="right">
<div class="name"><a href="<?php echo $urlProduktu; ?>"><?php echo $nazwa; ?></a></div>
<div class="typo-actions clearfix">
<div class="addtocart">
<a href="<?php echo $urlProduktu; ?>" class="button cart-links add_to_cart_button ajax_add_to_cart product_type_variable"><?php echo $zobacz; ?></a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile;
// Pagination
if ($pages > 1) {
repeater_pagination($page, $pages, 3);
}
?>
<?php else:
// no rows found
endif; ?>
</div>
</div>
</div>
和functions.php:
function repeater_pagination($paged = 1, $pages = '', $range = 2) {
$showitems = ($range * 2) + 1;
if(1 != $pages) {
echo "<ul class='pager'>";
// Arrows left
if ($paged > 1) echo "<li class='previous'><a href='".get_repeater_pagenum_link($paged - 1)."'><i class='icon-arrow-left'></i></a></li>";
// Numbers
for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
echo ($paged == $i)? "<li class='active'><a href='".get_repeater_pagenum_link($i)."'>".$i."</a></li>":"<li><a href='".get_repeater_pagenum_link($i)."'>".$i."</a></li>";
}
}
// Arrows right
if ($paged < $pages) echo "<li class='next'><a href='".get_repeater_pagenum_link($paged + 1)."'><i class='icon-arrow-right'></i></a></li>";
echo "</ul>\n";
}
}
function get_repeater_pagenum_link($pagenum = 1) {
global $wp_rewrite;
$pagenum = (int) $pagenum;
$pageslug = 'products';
$request = remove_query_arg('products');
$home_root = parse_url(home_url());
$home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
$home_root = preg_quote( trailingslashit( $home_root ), '|' );
$request = preg_replace('|^'. $home_root . '|', '', $request);
$request = preg_replace('|^/+|', '', $request);
// Normal Permalinks
if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
$base = trailingslashit( get_bloginfo( 'url' ) );
if ( $pagenum > 1 ) {
$result = add_query_arg('products', $pagenum, $base . `$request);`
} else {
$result = $base . $request;
}
// Pretty Permalinks
} else {
$qs_regex = '|\?.*?$|';
preg_match( $qs_regex, $request, $qs_match );
if ( !empty( $qs_match[0] ) ) {
$query_string = $qs_match[0];
$request = preg_replace( $qs_regex, '', $request );
} else {
$query_string = '';
}
$request = preg_replace( "|$pageslug/\d+/?$|", '', $request);
$request = preg_replace( '|^index\.php|', '', $request);
$request = ltrim($request, '/');
$base = trailingslashit( get_bloginfo( 'url' ) );
if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
$base .= 'index.php/';
if ( $pagenum > 1 ) {
$request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $pageslug . "/" . $pagenum, 'paged' );
}
$result = $base . $request . $query_string;
}
$result = apply_filters('get_repeater_pagenum_link', $result);
return $result;
}
/* Products pagination parameter */
function add_products_query_var(){
global $wp;
$wp->add_query_var('products');
}
/* Use EP_PERMALINK | EP_PAGES for pages and posts both */
function products_rewrite_endpoint(){
add_rewrite_endpoint('products', EP_ALL);
}
add_filter('init', 'add_products_query_var');
add_filter('init', 'products_rewrite_endpoint');
在模板中添加以下代码
<div class="container">
<div class="row">
<?php
$promocja = pll__('PROMOCJA!');
$zobacz = pll__('Zobacz produkt'); ?>
<div class="container productLoopContainer">
<?php
/* Template Loop */
// Get query var
global $wp_rewrite;
if ($wp_rewrite->using_permalinks()) {
if( !empty( get_query_var('products'))) {
$page = get_query_var('products');
} else {
$page = 1;
}
} else {
if( !empty( $_GET['products'] )) {
$page = $_GET['products'];
} else {
$page = 1;
}
}
// Variables
$i = 0;
$productspp = 8;
$products = get_field('produkt');
$total = count($products);
$pages = ceil($total / $productspp);
$min = (($page * $productspp) - $productspp) + 1;
$max = ($min + $productspp) - 1;
// Loop
if( have_rows('produkt')) { ?>
<div class="row">
<?php while(have_rows('produkt')): the_row(); $i++;
/* Ignore this row, if $i is lower than $min */
if($i < $min) {continue; }
/* Stop loop completely, if $i is higher than $max */
if($i > $max) {break; }
/* Close row after 3 products and start new one */
//if($c == 4): $c = 0;
// endif;
$kategoria = get_sub_field('kategoria');
$urlProduktu = get_sub_field('adres_url');
$zdjecie = get_sub_field('zdjecie');
$nazwa = get_sub_field('nazwa_produktu');
?>
<div class="col-sm-6 col-md-3">
<div class="product clearfix product-hover" id="<?php echo $kategoria; ?>">
<div class="left">
<div class="sale-badge"><?php echo $promocja; ?></div>
<div class="image">
<a href="<?php echo $urlProduktu; ?>"><img src="<?php echo $zdjecie; ?>" class="zoom-image-effect wp-post-image" alt="<?php echo $nazwa; ?>"></a>
</div>
</div>
<div class="right">
<div class="name"><a href="<?php echo $urlProduktu; ?>"><?php echo $nazwa; ?></a></div>
<div class="typo-actions clearfix">
<div class="addtocart">
<a href="<?php echo $urlProduktu; ?>" class="button cart-links add_to_cart_button ajax_add_to_cart product_type_variable"><?php echo $zobacz; ?></a>
</div>
</div>
</div>
</div>
</div>
<?php $c++; // column count
endwhile; ?>
</div>
<?php } else { ?>
<p><?php _e('No produkt found','mytextdomain'); ?></p>
<?php } ?>
<!-- Pagination -->
<?php if ($pages > 1) {
repeater_pagination($page, $pages, 3);
}
?>
</div>
</div>
</div>
在function.php中添加以下代码
<?php
/* ==========================================================================
ACF Repeater Field Pagination (in functions.php)
========================================================================== */
/**
* Custom bootstrap pagination for custom ACF repeater field array
*/
function repeater_pagination($paged = 1, $pages = '', $range = 2) {
$showitems = ($range * 2) + 1;
if(1 != $pages) {
echo "<ul class='pager'>";
// Arrows left
if ($paged > 1) echo "<li class='previous'><a href='".get_repeater_pagenum_link($paged - 1)."'><i class='icon-arrow-left'></i></a></li>";
// Numbers
for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
echo ($paged == $i)? "<li class='active'><a href='".get_repeater_pagenum_link($i)."'>".$i."</a></li>":"<li><a href='".get_repeater_pagenum_link($i)."'>".$i."</a></li>";
}
}
// Arrows right
if ($paged < $pages) echo "<li class='next'><a href='".get_repeater_pagenum_link($paged + 1)."'><i class='icon-arrow-right'></i></a></li>";
echo "</ul>\n";
}
}
/**
* Custom get_pagenum_link() function for custom paged links
*/
function get_repeater_pagenum_link($pagenum = 1) {
global $wp_rewrite;
$pagenum = (int) $pagenum;
$pageslug = 'products';
$request = remove_query_arg('products');
$home_root = parse_url(home_url());
$home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
$home_root = preg_quote( trailingslashit( $home_root ), '|' );
$request = preg_replace('|^'. $home_root . '|', '', $request);
$request = preg_replace('|^/+|', '', $request);
// Normal Permalinks
if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
$base = trailingslashit( get_bloginfo( 'url' ) );
if ( $pagenum > 1 ) {
$result = add_query_arg('products', $pagenum, $base . $request);
} else {
$result = $base . $request;
}
// Pretty Permalinks
} else {
$qs_regex = '|\?.*?$|';
preg_match( $qs_regex, $request, $qs_match );
if ( !empty( $qs_match[0] ) ) {
$query_string = $qs_match[0];
$request = preg_replace( $qs_regex, '', $request );
} else {
$query_string = '';
}
$request = preg_replace( "|$pageslug/\d+/?$|", '', $request);
$request = preg_replace( '|^index\.php|', '', $request);
$request = ltrim($request, '/');
$base = trailingslashit( get_bloginfo( 'url' ) );
if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
$base .= 'index.php/';
if ( $pagenum > 1 ) {
$request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( $pageslug . "/" . $pagenum, 'paged' );
}
$result = $base . $request . $query_string;
}
$result = apply_filters('get_repeater_pagenum_link', $result);
return $result;
}
/* ==========================================================================
Rewrite Rules
========================================================================== */
/* Products pagination parameter */
function add_products_query_var(){
global $wp;
$wp->add_query_var('products');
}
/* Use EP_PERMALINK | EP_PAGES for pages and posts both */
function products_rewrite_endpoint(){
add_rewrite_endpoint('products', EP_PERMALINK | EP_PAGES);
}
add_filter('init', 'add_products_query_var');
add_filter('init', 'products_rewrite_endpoint');
?>
<section id="inner-page">
<div class="inner-page testimonial_page">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$row= 0;
$testimonial_per_page = 1;
$testimonials = get_field( 'testimonial_section_data',get_the_ID() );
$total_testimonials = count( $testimonials );
$pages = ceil( $total_testimonials / $testimonial_per_page );
$min = ( ( $page * $testimonial_per_page ) - $testimonial_per_page ) + 1;
$max = ( $min + $testimonial_per_page ) - 1;
if( have_rows('testimonial_section_data',get_the_ID()) ):
while( have_rows('testimonial_section_data',get_the_ID()) ) : the_row();
$row++;
// Ignore this testimonials if $row is lower than $min
if($row < $min) { continue; }
if($row > $max) { break; }
$testimonial_section_name = get_sub_field('testimonial_section_name');
$testimonial_section_comment = get_sub_field('testimonial_section_comment');
$testimonial_section_review = get_sub_field('testimonial_section_review');
?>
<div class="testimonial-list">
<div class="testimonialpage_main">
<div class="testimonial_quote"><i class="fas fa-quote-left"></i></div>
<div class="testimonial_text">
<div class="review">
<?php echo $testimonial_section_review;?>
</div>
<?php echo $testimonial_section_comment;?>
<?php if($testimonial_section_name){?>
<h3>- <?php echo $testimonial_section_name;?></h3>
<?php } ?>
</div>
</div>
</div>
<?php
endwhile;
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'total' => $pages
) );
endif;
?>
</div>
</div>
</div>
</div>
</section>