我正在尝试为自定义帖子类型添加规则。默认情况下,要查看帖子的URL是www.mydomain.com/job/post-slug
我想要的是,帖子也可通过以下网址访问:
www.mydomain.com/j/postid
我已经在我的functions.php文件中尝试了这个,我还在管理设置中刷新了永久链接:
function rewrite_short_job_url() {
add_rewrite_rule( '^j/([0-9]+)/?', 'index.php?post_type=job&id=$matches[1]', 'top' );
flush_rewrite_rules( true );
}
add_action( 'init', 'rewrite_short_job_url' );
对我不起作用,我试图理解Rewrite API但找不到解决方案。
答案 0 :(得分:1)
我相信你应该使用以下内容:
function rewrite_short_job_url() {
add_rewrite_rule( '^j/([0-9]+)/?', 'index.php?post_type=job&p=$matches[1]', 'top' );
}
add_action( 'init', 'rewrite_short_job_url' );
保留post_type
var非常重要,这样只有职位才能使用该重定向。
您可以查看WordPress查询变量列表in the Codex。