重写产品固定链接后整个站点的分页不起作用

时间:2021-03-01 作者:Amy

我必须通过更改默认值来重写产品的永久链接/product/product-name 进入/%product-cat%/product-name. %product-cat% 是自定义类别。但在我重写之后,产品评论的分页就不起作用了。无论我点击哪个页面,url都是正确的,但评论总是停留在第一页。这是我第一次尝试进行permalink重写。我是否遗漏了任何内容或添加了错误的代码行,从而导致分页查询失败?

下面是我尝试重写的代码:

function product_perma_Rewrite() {

    $prodCat = [\'taxonomy\' => \'product_cat\'];
    $categories = get_categories($prodCat);
    $catSlug = [];
    foreach($categories as $category) {
        $catSlug[] = $category->slug;
    }
    add_rewrite_rule(
        \'^(\'.implode(\'|\', $catSlug).\')/([^/]*)/?\',
        \'index.php?post_type=product&category=$matches[1]&product=$matches[2]\',
        \'top\'
    );
    flush_rewrite_rules();
}
add_action(\'init\', \'product_perma_Rewrite\');

1 个回复
最合适的回答,由SO网友:Amy 整理而成

在汤姆的建议下,我设法把它修好了。这就是我尝试的解决方案。

function product_perma_Rewrite() {

    $prodCat = [\'taxonomy\' => \'product_cat\'];
    $categories = get_categories($prodCat);
    $catSlug = [];
    foreach($categories as $category) {
        $catSlug[] = $category->slug;
    }
    
     add_rewrite_rule(
        \'^(\'.implode(\'|\', $catSlug).\')/([^/]+)/comment-page-([0-9]{1,})/?$\',
        \'index.php?post_type=product&category=$matches[1]&product=$matches[2]&cpage=$matches[3]\',\'top\'
    );
    
    add_rewrite_rule(
        \'^(\'.implode(\'|\', $catSlug).\')/([^/]*)/?\',
        \'index.php?post_type=product&category=$matches[1]&product=$matches[2]\',
        \'top\'
    );
    
    flush_rewrite_rules();
}
add_action(\'init\', \'product_perma_Rewrite\');

相关推荐

Generate permalinks

我希望永久链接像youtube一样,由9位数的字母和数字生成我修改了这个代码add_filter( \'wp_unique_post_slug\', \'unique_slug_108286\', 10, 4 ); function unique_slug_108286( $slug) { $n=4; $slug = bin2hex(random_bytes($n)); //just an example return $slug; }&