如果存在其他参数,则从自定义帖子类型重写 url 后找不到错误 404 页面

这是自定义帖子类型的默认链接:


domain.com/course/mern-stack-front-to-back-full-stack-react-redux-node-js

如果存在的参数是 meta_id,我正在尝试创建自定义重写规则


domain.com/course/mern-stack-front-to-back-full-stack-react-redux-node-js?meta_id=1234

应该变成这样:


domain.com/course/mern-stack-front-to-back-full-stack-react-redux-node-js/lesson/1234

这是我在 functions.php 中的代码


function custom_rewrite_tag() {

    add_rewrite_tag( '%meta_id%', '([^&]+)' );

}

add_action('init', 'custom_rewrite_tag', 10, 0);


function custom_rewrite_rule() {

    add_rewrite_rule( '^lesson/([0-9]+)/?', 'index.php?post_type=course&meta_id=$matches[1]', 'top' );

}

add_action('init', 'custom_rewrite_rule', 10, 0);


米脂
浏览 96回答 1
1回答

ITMISS

function custom_rewrite_tag() {    add_rewrite_tag( '%meta_id%', '([^&]+)' );}add_action('init', 'custom_rewrite_tag', 10, 0);function custom_rewrite_rule() {    add_rewrite_rule('^course/([^/]*)/lesson/([0-9]+)/?','index.php?post_type=course&name=$matches[1]&meta_id=$matches[2]', 'top');}add_action('init', 'custom_rewrite_rule', 10, 0);
打开App,查看更多内容
随时随地看视频慕课网APP