我正在使用add_rewrite_rule()函数来修改我的URL结构。
我想使用add_rewrite_rule添加自定义规则,但只有在我的永久链接设置区域中选择了默认设置以外时才会添加这些规则。
即。在设置中有以下选项:
- Default http://localhost/wordpress/?p=123
- Day and name http://localhost/wordpress/2014/08/14/sample-post/
- Month and name http://localhost/wordpress/2014/08/sample-post/
- Numeric http://localhost/wordpress/archives/123
- Post name http://localhost/wordpress/sample-post/
- Custom Structure http://localhost/wordpress
因此,当我选择其他'默认'时,我的add_rewrite_rule()函数可以正常工作,但在选择'默认'时,该功能似乎无法正常工作。所以请建议我如何在任何条件下工作。任何帮助都是Appriciated。
我认为问题在于: 当我使用它时,选择'默认':
get_option('permalink_structure');
我一无所获。
在其他情况下,有一些值,如:
/%postname%/
/archives/%post_id%
/%year%/%monthnum%/%postname%/
答案 0 :(得分:2)
默认永久链接,或者所谓的" Ugly"永久链接,没有向.htaccess文件添加任何内容,因此未启用Apache重写引擎。没有重写引擎,就不能重写。所以简短的回答是默认永久链接无法重写。
我可以建议您使用重写以及查询变量。添加重写规则时,将自定义数据传递给查询var,并围绕该查询var构建功能。这样,您的功能将适用于所有情况和所有永久链接类型。
例如,如果您有以下规则:
add_rewrite_rule('^sometest/([^/]*)/?','index.php?custom_query_var=$matches[1]', 'top');
并使用以下代码将custom_query_var
添加为查询var:
function add_query_vars_filter( $vars ){
$vars[] = "custom_query_var";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
然后选择默认永久链接时,以下网址适用于您:
http://yoursite.com/index.php?custom_query_var=abc
如果"漂亮"选择永久链接后,URL重写将起作用,您的URL将按以下方式显示:
http://yoursite.com/sometest/abc/
这基本上是重写可以达到的最佳效果。
答案 1 :(得分:2)
我同意@Martin。这是一个有助于https://core.trac.wordpress.org/ticket/15235
的资源答案 2 :(得分:0)
use this:
function my_add_query_vars( $qvars ) {
$qvars[] = 'business-coaching';
$qvars[] = 'country';
$qvars[] = 'territory';
$qvars[] = 'region';
return $qvars;
}
add_action('query_vars', 'my_add_query_vars');
//Write the rule
function add_analytic_rewrite_rule()
{
// Regex:The regex to match the incoming URL is:business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?
// Redirect Rule :The resulting internal URL: `index.php` because we still use WordPress
// `pagename` or page_id=45 because we use this WordPress page
// `country` : we will assign the first captured regex part to this variable
// `territory` we will assign the second captured regex part to this variable
// `region` we will assign the third captured regex part to this variable
add_rewrite_rule('business-coaching(/([^/]+))?(/([^/]+))?(/([^/]+))?/?','index.php?page_id=45&country=$matches[2]&territory=$matches[`enter code `enter code here`here`4]®ion=$matches[6]','top');//superfinal
}
add_action('init', 'add_analytic_rewrite_rule');