简体   繁体   English

使用add_rewrite_rule()或generate_rewrite_rules()的Wordpress URL重写自定义分类法

[英]Wordpress URL rewrite custom taxonomy using add_rewrite_rule() or generate_rewrite_rules()

I'm trying to get my head around amending some URL's for a website I have created. 我正在设法修改我创建的网站的某些URL。 After reading two (very informative) answers ( 1 , 2 ) but I'm still a little unsure how to approach creating a URL that shows a custom post type in the hierachy of it's categories (created by custom taxonomies). 读取两个(非常丰富)答案(后12 ),但我还是有点不知道该如何着手制作,显示的层次结构的自定义职位类型是类别的URL(通过自定义分类创建)。

From my reading, I gather that add_rewrite_rule or generate_rewrite_rules seem to be the best options to use, but I cannot seem to get it functional... like... at all! 从我的阅读中,我发现add_rewrite_rulegenerate_rewrite_rules似乎是最好的选择,但是我似乎无法使其正常工作……就像……! Here is the URL structure I have currently : 这是我目前拥有的URL结构

Base        = http://domain.com/bands
Category    = http://domain.com/band-type/acoustic-duos
Single Post = http://domain.com/bands/duo-1

And here is what I am looking to achieve : 这是我希望实现的目标

Base        = http://domain.com/bands
Category    = http://domain.com/bands/acoustic-duos
Single Post = http://domain.com/bands/acoustic-duos/duo-1

In the scenario above, bands is the custom-post-type , whilst band-type is the custom-taxonomy I have created for 'bands'. 在上面的场景中, bands是custom-post-type ,而band-type我为“ bands”创建的定制分类法

From using @jan-fabry Rewrite Analyzer plugin, I can see the custom taxonomy pattern is: band-type/([^/]+)/?$ for the first argument for add_rewrite_rule() but I don't know how Wordpress references custom taxonomies in the index.php URL variable, used for the $redirect argument (eg index.php?tax=bands )... so unsurprisingly the following code (placed within functions.php ) did not work at all: 通过使用@ jan-fabry Rewrite Analyzer插件,我可以看到自定义分类模式是: add_rewrite_rule() band-type/([^/]+)/?$作为add_rewrite_rule()的第一个参数,但是我不知道Wordpress如何引用用于$redirect参数的index.php URL变量中的自定义分类法(例如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;
}

I still feel I'm not grasping how this works (at all!) and reading the codex is confusing me further so I'm not sure how to approach using generate_rewrite_rules() . 我仍然觉得我根本不了解它是如何工作的,而且阅读法典会使我更加困惑,因此我不确定如何使用generate_rewrite_rules()

Lastly, I'm wondering if I'm fooling foul of a permalink conflict, as I want the custom-taxonomy (band-type) to use the custom-post-type's slug ('bands') as the baseline for the URL. 最后,我想知道是否要避免永久链接冲突,因为我希望自定义分类法(带型)使用自定义帖子类型的子弹(“带”)作为URL的基线。

Please can you point me in the right direction, I can provide more information if necessary. 请您指出正确的方向,如有必要,我可以提供更多信息。 Thanks. 谢谢。

This can all be done with your .htaccess file. 所有这些都可以通过您的.htaccess文件来完成。 This assumes the .htacces file is in your root directory and that wordpress is installed in the root directory and not a subfolder. 这假定.htacces文件位于您的根目录中,并且wordpress安装在根目录中,而不是子文件夹中。 If you installed wordpress in a subfolder change the RewriteBase to: 如果您在子文件夹中安装了wordpress,请将RewriteBase更改为:

RewriteBase http://domain.com/subfolder/

Where subfolder is the subdirectory where you installed wordpress. 其中subfolder是您安装wordpress的子目录。 For all else use the code below where the wordpress install is in the root directory. 对于其他所有代码,请使用下面的代码,其中wordpress安装位于根目录中。

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteRule ^band-type/acoustic-duos/(.*)$ bands/acoustic-duos/$1 [QSA,L]
RewriteRule ^bands/(.*)$ bands/acoustic-duos/$1 [QSA,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Be careful when editing the .htaccess file. 编辑.htaccess文件时要小心。 Make sure to save a backup of your current .htaccess before editing using the code above. 使用上述代码进行编辑之前,请确保保存当前.htaccess的备份。 I believe the first RewriteRule should work. 我相信第一个RewriteRule应该可以工作。 the second RewriteRule may cause a redirect loop or your site to crash because we are redirecting two urls: 第二个RewriteRule可能会导致重定向循环或您的网站崩溃,因为我们正在重定向两个URL:

/bands/duo-1 
/band-type/acoustic-duos 

to have a new base of: 有一个新的基础:

/bands/acoustic-duos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM