我想在WordPress中使用自定义网址。WordPress的自定义网址和GET
http://example.com/home代替http://example.com?page_id=home
但如何我现在应该使用GET功能?
$page_id = $_GET['page_id'];
if($page_id == 'home') include 'home.php';
我想在WordPress中使用自定义网址。WordPress的自定义网址和GET
http://example.com/home代替http://example.com?page_id=home
但如何我现在应该使用GET功能?
$page_id = $_GET['page_id'];
if($page_id == 'home') include 'home.php';
$_GET
不是一个函数,你不应该使用它。它包含从查询字符串解析的值,并且您没有。
解析$_SERVER['REQUEST_URI']
。
您可以像http://localhost/wordpress/2012/01/hello-world/?g=1
这个URL使用。此网址的print_r($_GET)
的结果是Array ([g] => 1)
。
但是,如果你想使用自定义url,你必须改变htaccess文件。您可以使用下面两个选项:
的“WordPress的办法”要做到这一点是将page_id
注册为“查询变量”,并使用get_query_var
。
要注册查询变量,您可以使用query_vars
过滤器。把下列句子变成你的插件或主题的function.php
add_filter('query_vars', 'register_my_query_vars');
function register_my_query_vars($qvars){
//Add these query variables - do not overwrite the $qvars array
$qvars[] = 'page_id';
return $qvars;
}
那么你应该能够使用get_query_var('page_id')
获得它的价值(家庭,在这个问题)。