WordPress AMP的自定义帖子类型与自己的URL


WordPress AMP for Custom Post Types with own URL

我想在WordPress中使用自定义帖子类型的AMP。因此,我在functions.php中添加了以下代码:

add_action( 'amp_init', 'xyz_amp_add_review_cpt' );
function xyz_amp_add_review_cpt() {
    add_post_type_support( 'xyz-review', AMP_QUERY_VAR );
}

代码来自官方插件文档:https://github.com/Automattic/amp-wp/blob/master/readme.md#custom-post-type-support

之后,我已经刷新了重写规则。不幸的是,这对我的CPT不起作用。仅适用于普通职位。

我想这是因为我的CPT没有slug。URL与帖子相同:example.com/custom-post-type-post/

如果我添加一个段塞到CPT, AMP将工作。但是我不能那样做。

是否有其他解决方案来解决这个问题?

试试这个

add_rewrite_rule( '^([^/]+)+['/]+(amp)/?$', 'index.php?post_type=xyz-review&name=$matches[1]&amp=1', 'top' );