2013-01-20 46 views
1

我尝试为我的自定义帖子类型创建重写规则。我需要它是类似“/%post_type_name%/%taxonomy_slug /%post_slug%”。这里是我用来添加规则的代码:无法创建重写规则

add_action('init', 'episodes_rewrites_init'); 

function episodes_rewrites_init(){ 
    add_rewrite_rule(
     '^episodes/([^/]*)/([^/]*)/?$', 
     'index.php?post_type=episodes&seasons=$matches[1]&episodes=$matches[2]', 'top' 
    ); 
} 

它不起作用,我不知道为什么。请帮忙。

回答

0

好吧,我刚刚安装的插件添加网址模板为我的自定义后,如“/%季节%/%邮政名称%/“,现在它会像我想要的那样重写URL。但我仍然不知道如何在代码中做到这一点...

0

好的队友,我从来没有用过add_rewrite_rule,WP codex说的是It is most commonly used in conjunction with add_rewrite_tag()
所以我只是向你展示退伍军人在过去如何执行这种重写规则。
但是,如果你正在为群众建立一个插件,你必须避免使用WP 3.5 features许多人仍然使用WP2。*。

这组动作可以帮助你:

所谓的“自定义文章类型固定链接”,
add_action('init', 'flushRewriteRules'); 
function flushRewriteRules() 
{ 
    global $wp_rewrite; 
    $wp_rewrite->flush_rules(); 
} 

add_filter('rewrite_rules_array', 'insertMyRewriteRules'); 
function insertMyRewriteRules($rules) 
{ 
    $newrules['^episodes/([^/]*)/([^/]*)/?$'] = 'index.php?post_type=episodes&seasons=$matches[1]&episodes=$matches[2]'; 
    return $newrules + $rules; 
} 

add_filter('query_vars', 'insertMyRewriteQueryVars'); 
function insertMyRewriteQueryVars($vars) 
{ 
    array_push($vars, 'episodes', 'seasons', 'post_type'); 
    return $vars; 
} 
+0

仍然无法正常工作。 –