dongsong4418 2019-02-27 22:49
浏览 26

在没有前缀/类别/的子类别中使用add_rewrite_rule

I created this filter with the intention of creating URLs for the AMP version of my website:

function r_amp_add_rewrite_rules() {
    global $wp_rewrite;

    add_rewrite_rule('([^/]+)/amp/?$',
    'index.php?amp=1&category_name=$matches[1]',
    'top');

    add_rewrite_rule('([^/]+)/amp/'.$wp_rewrite->pagination_base.'/?([0-9+])/?$',
    'index.php?amp=1&category_name=$matches[1]&paged=$matches[2]',
    'top');

    add_rewrite_rule('([^/]+)/([^/]+)/amp/?$',
    'index.php?amp=1&category_name=$matches[1]/$matches[2]',
    'top');

    add_rewrite_rule('([^/]+)/([^/]+)/amp/'.$wp_rewrite->pagination_base.'/?([0-9]{1,})/?$',
    'index.php?amp=1&category_name=$matches[1]/$matches[2]&paged=$matches[3]',
    'top');

    add_rewrite_rule('([^/]+)/amp/?$',
    'index.php?amp=1&pagename=$matches[1]',
    'top');

    add_rewrite_rule('amp/?$',
    'index.php?amp=1&page_id=7',
    'top');

}
add_filter('init', 'r_amp_add_rewrite_rules', 9999);

function r_amp_query_vars( $vars ) {
    array_push($vars, 'amp');
    return $vars;
}
add_filter( 'query_vars','r_amp_query_vars' );

Everything works fine, except one thing... I have Yoast SEO installed, with the option to remove the prefix "category" from the slugs of the categories and I also have categories with parent, ie categories with urls of this type:

/slug-main/name-category-child/

With the yoast seo option to remove the /category/ AMP url gives error 404.

/slug-main/name-category-child/amp/

And with the option deactivated, there is no error, but I want to remove the prefix category...

/category/slug-main/name-category-child/amp/

What can I do about it?

I have tried to install this plugin: https://wordpress.org/plugins/accelerated-mobile-pages/ and try if the same thing happened, and yes, it also happens... Although I want to fix it in my code, since I'm doing the AMP version of my theme without plugins...

Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大