如何重写 wordpress URL 以使用第一个子目录作为查询字符串? [英] How can I rewrite a wordpress URL to use the first sub directory as a querystring?

查看:59
本文介绍了如何重写 wordpress URL 以使用第一个子目录作为查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果类别与程序代码匹配,我需要 URL 中的第一个子目录重写为查询字符串 URL,即:4 个大写字符,例如 AUBU

I need the first sub-directory in my URL to rewrite to a querystringed URL if the category matches a program code ie: 4 upper case characters such as AUBU

例如,我需要:

http://www.georgiancollege.ca/devprograms/AUBU/

重写为:

http://www.georgiancollege.ca/devprograms/index.php?page_id=16&major=AUBU

但我不希望它影响当前的 URL,例如

But I don't want it to affect current URLs such as

http://www.georgiancollege.ca/devprograms/a-to-z/

哪些不应该被重写:

http://www.georgiancollege.ca/devprograms/index.php?page_id=16&major=a-to-z

这是我到目前为止根本无法使用的内容.(基于:http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/ )

Here is what I have so far which is not working at all. (based on: http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/ )

function eg_add_rewrite_rules() {
global $wp_rewrite;

$new_rules = array(
    '(.+)/?$' => 'index.php?page_id=16&major=' . $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );

更新:上面的代码现在重定向到正确的页面,但在该页面上我无法读取查询字符串,可能是因为 URL 重写,查询字符串不在最后一页上......

UPDATE: The code above now redirects to the proper page, but on that page I can't read the querystring in, likely because of the URL rewrite the querystring isn't on the final page...

所以使用,

$program = $_GET['major'];

不返回主要代码...

推荐答案

感谢您的输入.

我不得不使用变通"解决方案,因为它似乎不想工作.

I've had to use a 'work around' solution because it just doesn't seem to want to work.

我尝试使用 $wp_query->query_vars 来获取查询字符串数据,而不是 $_GET 或 $_POST 像这样:

I tried using $wp_query->query_vars to get the querystring data as opposed to $_GET or $_POST like so:

if(isset($wp_query->query_vars['major'])) {
    $program = urldecode($wp_query->query_vars['major']);
}

如建议:http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ 但主要"的 query_var 仍然不存在,即使来自同一 URL 的页面名称查询变量重写确实存在.

As suggested on: http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ but the query_var for 'major' kept being non existent even though the pagename query var from the same URL rewrite did exist.

所以我继续前进......

So I moved on...

这是新的 URL 重写:

Here is the new URL rewrite:

function add_rewrite_rules($aRules) {
  $aNewRules = array('([A-Z]{4})/outline/?$' => 'index.php?pagename=programs&major=$matches[1]');
  $aNewRules2 = array('([A-Z]{4})/?$' => 'index.php?pagename=programs&major=$matches[1]');
  $aRules = $aNewRules + $aNewRules2 + $aRules;
  return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');

由于我的 URL 重写仍然以更漂亮的方式将程序代码保留在 URL 中,因此我只是获取 REQUEST_URI 并像这样解析程序代码:

Since my URL rewrite is still keeping the program code in the URL just in a prettier way, I'm simply grabbing the REQUEST_URI and parsing out the program code like so:

$parts = explode('/',$_SERVER['REQUEST_URI']);
if (isset($_GET['major'])) {
  $program = $_GET['major']; 
} elseif ($parts[1] == 'devprograms') {
  $program = $parts[2];
} else {
  $program = get_the_title();
}

这说明了链接样式/devprograms/BUSN 和/devprograms/programs?major=BUSN

This accounts for both link styles /devprograms/BUSN and /devprograms/programs?major=BUSN

唯一的缺点是我不能在安装中使用其他带有 4 个字母名称的页面,例如test"或page",因为它们将被重写为test"或page"作为程序的程序页面代码,这不会是实际的程序.这是一个在命名页面时很容易解决的问题.

The only downside to this is I cannot have other pages on the install with 4 letter names such as 'test' or 'page' as they will get rewritten to the programs page with 'test' or 'page' as the program code, which won't be actual programs. That is an issue that is easy to work around when naming pages though.

谢谢,托马斯

这篇关于如何重写 wordpress URL 以使用第一个子目录作为查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆