简体   繁体   English

Wordpress-URL字符串上的额外“ pretty”参数。 Mod_rewrite?

[英]Wordpress - extra 'pretty' parameter on the URL string. Mod_rewrite?

I'm currently developing a WordPress site that has an unusual taxonomy. 我目前正在开发一个具有非常规分类法的WordPress网站。 In order to get around certain issues I need to add a parameter to the end of the URL. 为了解决某些问题,我需要在URL的末尾添加一个参数。 I have this working, however I need some way of using mod_rewrite to make the parameter pretty. 我有这个工作,但是我需要一些使用mod_rewrite使参数漂亮的方法。

Here is what I currently have... 这是我目前拥有的...

www.site.com/('director-name' or 'project-name')/?path=('visual' or 'production') www.site.com/('director-name'或'project-name')/?path =('visual'或'production')

for example: www.site.com/joe-smith/?path=visual 例如:www.site.com/joe-smith/?path=visual

I am using the $_GET to access this variable for my functionality, and it works fine. 我正在使用$ _GET来访问此变量以实现其功能,并且工作正常。 The issue is I want to make the URL read... 问题是我想使URL读取...

www.site.com/joe-smith/visual OR www.site.com/art/production www.site.com/joe-smith/visual或www.site.com/art/production

I have read that messing with the .htaccess in WordPress isn't the best way to deal with this. 我读过,弄混WordPress中的.htaccess并不是解决此问题的最佳方法。

Help will be much appreciated. 帮助将不胜感激。 Thanks! 谢谢!

You don't need to do anything with .htaccess or mod_rewrite since you are using Wordpress. 由于您使用的是Wordpress,因此您无需对.htaccess或mod_rewrite进行任何操作。 Wordpress has a built in system for handling rewrite rules. Wordpress具有用于处理重写规则的内置系统。 You can also set up your URL parameters right in the function. 您也可以直接在函数中设置URL参数。 The function is add_rewrite_rule() and the example in the codex is pretty good. 该函数是add_rewrite_rule()并且抄本中的示例非常好。 I can walk you through it, though, so you can see exactly what is going on. 不过,我可以指导您完成操作,以便您可以确切地了解发生了什么。

Take the following rewrite rule from the example: 从示例中采用以下重写规则:

add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');

First, lets go over the function arguments. 首先,让我们回顾一下函数参数。 The first argument is the regex for your rewrite. 第一个参数是用于重写的正则表达式 If this regex finds a match on the current URL, your rewrite will kick in. Any URL parameters will be loaded into a $matches array to be used later in the second argument of the function. 如果此正则表达式在当前URL上找到匹配项,则将开始您的重写。所有URL参数都将加载到$matches数组中,以便稍后在该函数的第二个参数中使用。

The second argument is actual URL that will be rewritten. 第二个参数是将要重写的实际URL。 Note that the page_id is being used here and not the page slug. 请注意,此处使用的是page_id,而不是页面标签。 It is important that you use the page ID for this parameter and not the slug since the slug itself is a rewrite. 重要的是,请使用页面ID作为此参数而不是Slug,因为Slug本身是重写的。 The latter parameters are the values for the querystring that you are passing to your handler. 后面的参数是您要传递给处理程序的查询字符串的值。

As you may know, all pages in Wordpress are routed through the index.php file. 如您所知,Wordpress中的所有页面都通过index.php文件进行路由。 If you are familiar with MVC this is very much like the front controller. 如果您熟悉MVC,则非常类似于前端控制器。 As such, all of your rewrites should contain the index.php file at the beginning. 因此,您的所有重写都应在开始时包含index.php文件。 This is reflected in the second argument in this function. 这反映在此函数的第二个参数中。 You can have custom rewrites that use other files, but for the sake of simplicity, let's assume that you are using the standard Wordpress index.php file. 您可以使用其他文件进行自定义重写,但是为了简单起见,我们假设您使用的是标准的Wordpress index.php文件。

The final argument is the position in the queue that the rewrite should be placed. 最后一个参数是队列中应放置重写的位置。 You can either use "top" or "bottom". 您可以使用“顶部”或“底部”。 Top being loaded before Wordpress' rewrites and bottom being after. 顶部在Wordpress重写之前被加载,底部在之后被加载。

Using this function Wordpress will add lines to your htaccess on-the-fly and update them based on the current page. 使用此功能,Wordpress将即时向您的htaccess添加行并根据当前页面对其进行更新。 This is definitely the best way to do rewrites in Wordpress! 这绝对是在Wordpress中进行重写的最佳方法! Hope this helps! 希望这可以帮助!

UPDATE BASED ON COMMENTS 根据评论更新

You would place this code in your functions.php file. 您可以将此代码放置在functions.php文件中。 You should wrap it in a function and hook it to an action that fires before the rewrite rules. 您应该将其包装在函数中,并将其挂钩到在重写规则之前触发的操作。 Here is an example that matches your exact issue: 这是一个与您的确切问题相匹配的示例:

add_action('init', 'my_custom_rewrite_function');
function my_custom_rewrite_function() {
    $director_category_id = 5; // Change to correct id
    $project_category_id = 6; // Change to correct id
    $handler_page_id = 25 // The page ID for the handler

    add_rewrite_rule('^director/([^/]*)/([^/]*)/?','index.php?page_id=$handler_page_id&category=$director_category_id&name=$matches[1]&path=$matches[2]','top');
    add_rewrite_rule('^project/([^/]*)/([^/]*)/?','index.php?page_id=$handler_page_id&category=$project_category_id&name=$matches[1]&path=$matches[2]','top');

    flush_rewrite_rules(); // This will make sure that the rewrite rules are added
}

You can then handle your query in your custom template by doing a get_query_var() for each of your querystring arguments. 然后,您可以通过对每个querystring参数执行get_query_var()来处理自定义模板中的查询。

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

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