WordPress的add_rewrite_rule()

时间:2019-02-21 13:39:41

标签: wordpress mod-rewrite

我在主题书中使用默认的帖子类型,当我创建任何自定义帖子时,它会提供/ book / book1 /之类的url,而我想要/ book1 /。我在functions.php中添加了一些自定义代码,首先将/ book / book1 /替换为/ book1 /,然后使用add_rewrite_rule()函数重写url。 //代码

enter code here

function gp_remove_cpt_slug($ post_link,$ post){

if('book'=== $ post-> post_type &&'publish'=== $ post-> post_status){         $ post_link = str_replace('/'。$ post-> post_type。'/','/',$ post_link);

}

return $post_link;

}

add_filter('post_type_link','gp_remove_cpt_slug',10,2);

function custom_rewrite_basic(){

add_rewrite_rule('^/([0-9]+)/([^/]+)/?', 'index.php?book=$matches[1]', 'top');

}

add_action('init','custom_rewrite_basic',20); 请告诉我该怎么做?

0 个答案:

没有答案