【发布时间】:2017-11-06 09:53:50
【问题描述】:
我在我的网站上使用高级自定义字段插件来展示产品。显示 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');
【问题讨论】:
-
您可以尝试在主页或博客页面上使用 'paged' 而不是 'page' (get_query_var)。
-
@YuxelYuseinov 不幸的是,问题仍然存在。
-
你的帖子类型 slug 名称是什么?
-
这是我的主页模板,名为 /* 模板名称:SG */
标签: php wordpress pagination advanced-custom-fields