【发布时间】:2014-09-22 17:19:22
【问题描述】:
我正在努力为我创建的网站修改一些 URL。在阅读了两个(非常丰富的)答案(1,2)之后,但我仍然有点不确定如何创建一个 URL,该 URL 在其类别的层次结构中显示自定义帖子类型(由自定义分类法创建)。
根据我的阅读,我认为add_rewrite_rule 或generate_rewrite_rules 似乎是最好的选择,但我似乎无法让它发挥作用......就像......根本! 这是我目前的 URL 结构:
Base = http://domain.com/bands
Category = http://domain.com/band-type/acoustic-duos
Single Post = http://domain.com/bands/duo-1
这就是我想要实现的目标:
Base = http://domain.com/bands
Category = http://domain.com/bands/acoustic-duos
Single Post = http://domain.com/bands/acoustic-duos/duo-1
在上面的场景中,bands 是自定义帖子类型,而 band-type 是自定义分类我为“乐队”创建的。
通过使用@jan-fabry Rewrite Analyzer 插件,我可以看到自定义分类模式是:band-type/([^/]+)/?$ 用于add_rewrite_rule() 的第一个参数,但我不知道 Wordpress 如何在 index.php URL 中引用自定义分类变量,用于$redirect 参数(例如index.php?tax=bands)...所以不出所料,以下代码(放在functions.php 中)根本不起作用:
wp_reset_query();
function sheixt_init()
{
add_rewrite_rule(
'band-type/([^/]+)/?',
'index.php?cat=&band-type=$matches[1]',
'top' );
}
add_filter( 'query_vars', 'sheixt_query_vars' );
function sheixt_query_vars( $query_vars )
{
$query_vars[] = 'band-type';
return $query_vars;
}
我仍然觉得我没有掌握它是如何工作的(根本!)并且阅读法典让我更加困惑,所以我不确定如何使用generate_rewrite_rules()。
最后,我想知道我是否在犯永久链接冲突,因为我希望自定义分类法(band-type)使用 custom-post-type 的 slug('bands')作为基线网址。
请您指出正确的方向,如有必要,我可以提供更多信息。谢谢。
【问题讨论】:
标签: wordpress .htaccess mod-rewrite url-rewriting