【问题标题】:Another problem with 404 in pagination WooCommerce分页 WooCommerce 中 404 的另一个问题
【发布时间】:2019-04-03 08:21:34
【问题描述】:

看了这么多文章,没有找到答案。

我在类别中的自定义产品循环:

$paged = get_query_var('paged') ? get_query_var('paged') : 1;
global $post;
$args = array(
'posts_per_page' => 6,
'product_cat' => $post->post_name, // **The father category**
'post_type' => 'product',
'paged' => $paged,
'page' => $page,
'pagination' => true
);
$loop = new WP_Query( $args );
echo $loop->request;
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
    global $product;
    ?>

////////////////// product info//////////////

    <?php endwhile;
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $loop->max_num_pages
    ) );
} else {
    echo __( 'No products found' );
}
wp_reset_postdata();

在分类页面我有相同的链接:

http://localhost:3000/product-category/cats/videocard/

分页链接: http://localhost:3000/product-category/cats/videocard/page/2/

但我上面有 404。

我的永久链接设置: enter image description here

我的“阅读”设置: enter image description here

我做错了什么?

【问题讨论】:

  • 我将此规则添加到 .htaccess RewriteRule ^page/([^/]*)/$ /?page=$1 [L] 如果我转到 localhost:3000/product-category/cats/videocard/?page=2 我会看到第 2 页。如何我可以在 localhost:3000/product-category/cats/videocard/page/2 中获得相同的结果吗?

标签: php wordpress woocommerce pagination


【解决方案1】:

您的问题可能与 WP_Query 参数有关。我发现了以下问题:

  • 'pagination' 不是有效参数,
  • 作为键给出的分类参数 ('product_cat') 已弃用,您应该改用 'tax_query'
  • $page 变量值未定义(至少在您的示例脚本中未定义),似乎 'page'' 的定义paged' 参数可能会导致您的查询发生冲突。

尝试使用以下方法:

global $post;
$args = array(
    'posts_per_page' => 6,
    'post_type'      => 'product',
    'paged'          => get_query_var('paged') ? get_query_var('paged') : 1,
    'tax_query'      => array(
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => $post->post_name // !
        ),
    ),
);
$loop = new WP_Query( $args );
// echo $loop->request;  - I guess this is a test left-over
if ( $loop->have_posts() ) {

    while ( $loop->have_posts() ) : $loop->the_post();
        /**
         * No need to call the $product global within the loop,
         * use the $loop->post or even better call the product object
         * using the WooCommerce wc_get_product() function as displayed
         * below
         */
        // global $product;
        $product = wc_get_product( $loop->post );
    ?>

////////////////// product info//////////////

    <?php endwhile;
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base'    => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format'  => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total'   => $loop->max_num_pages
    ) );

} else {

    echo __( 'No products found' );
}
wp_reset_postdata();

最后但并非最不重要的一点是,您为 product_cat 提供的分类术语值(注释为 父类别)是当前的帖子 slug。我理解您为什么要这样做,但在某些情况下,这也可能是您获得这些 404 的原因。因此,如果上述脚本不能解决您的问题,我建议您也考虑一下。

【讨论】:

猜你喜欢
  • 2011-07-09
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多