php $_GET 和 WordPress
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1018935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$_GET and WordPress
提问by marcgg
I want to add a custom php file to a WordPress to do a simple action.
我想将自定义 php 文件添加到 WordPress 以执行简单操作。
So far I have in my theme index.phpfile:
到目前为止,我的主题index.php文件中有:
<a href="myfile.php?size=md">link</a>
and the php is
和 php 是
<?php echo "hello world"; ?>
<?php echo $_GET["size"]; ?>
<?php echo "hello world"; ?>
The link, once clicked, displays:
单击该链接后,将显示:
hello world
Is WordPress taking over the $_GETfunction and I need to do some tricks to use it? What am I doing wrong?
是 WordPress 接管了这个$_GET功能,我需要做一些技巧来使用它吗?我究竟做错了什么?
Edit:
编辑:
<?echo "hello world";?>
<?
if (array_key_exists('size', $_GET))
echo $_GET['size'];
?>
<?echo "end";?>
Ouputs :
输出:
hello world
采纳答案by Zenshai
Not sure if this will show anything but try turning on error reporting with:
不确定这是否会显示任何内容,但尝试打开错误报告:
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
?>
at the top of your page before any other code.
在任何其他代码之前的页面顶部。
Edit:
编辑:
From the OP comments:
从 OP 评论:
silly question, but are you sure you are viewing the results of your latest changes to the file and not a cached copy of the page or something? Change "hello world" to something else. (Sorry grasping at straws, but this happened to me before) – Zenshai
ahaha, the person that were doing the changes didn't changed the correct file. It's working now – marcgg
peer programming fail ^^ – marcgg
That would be an "or something", can't tell you how many times i've done something like that. Glad you were able to figure it out in the end. – Zenshai
愚蠢的问题,但是您确定您正在查看对文件的最新更改的结果,而不是页面的缓存副本或其他内容吗?将“hello world”更改为其他内容。(对不起,抓住了稻草,但这以前发生在我身上)– Zenshai
啊哈哈,进行更改的人没有更改正确的文件。现在开始工作了 – marcgg
对等编程失败^^ – marcgg
那将是“或某事”,无法告诉您我做过多少次这样的事情。很高兴你最终能够弄清楚。– 禅师
I usually discover errors like these only when they begin to defy everything I know about a language or an environment.
我通常只有在这些错误开始违背我对语言或环境的了解时才会发现这些错误。
回答by TitiPotter
See the solution :
请参阅解决方案:
In order to be able to add and work with your own custom query vars that you append to URLs, (eg: www.site.com/some_page/?my_var=foo- for example using add_query_arg()) you need to add them to the public query variables available to WP_Query. These are built up when WP_Queryinstantiates, but fortunately are passed through a filter query_varsbefore they are actually used to populate the $query_varsproperty of WP_Query.
为了能够添加和使用您自己附加到 URL 的自定义查询变量,(例如:www.site.com/some_page/?my_var=foo-例如使用add_query_arg()),您需要将它们添加到WP_Query. 这些都是建立在WP_Query实例化,但幸运的是穿过过滤器query_vars之前,他们实际上是用来填充$query_vars的财产WP_Query。
For your case :
对于您的情况:
function add_query_vars_filter( $vars ){
$vars[] = "size";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
and on your template page call the get methode like that :
并在您的模板页面上调用这样的 get 方法:
$size_var = (get_query_var('size')) ? get_query_var('size') : false;
if($size_var){
// etc...
}
More at the Codex: http://codex.wordpress.org/Function_Reference/get_query_var
更多在 Codex:http: //codex.wordpress.org/Function_Reference/get_query_var
I hope it helps !
我希望它有帮助!
回答by Regis Zaleman
Wordpress does not take $_GET over. Are you sure that you are passing the variable correctly?
Wordpress 不会接管 $_GET。你确定你正确地传递了变量吗?
If you hardcode the variable in a url, make sure it is of this form:
如果您在 url 中对变量进行硬编码,请确保它采用以下形式:
YOUR_SITE_PATH/?variable_name=variable_value
please not the "/" at the end of the url, before the "?"
请不要在网址末尾的“/”,在“?”之前
I cannot see your index.php code, but make sure that the variable "size" is either set manually in the url or from a submitted form. If you use a form, make sure that you use the method="GET". If you use method="POST", then your variable will be in $_POST['size'].
我看不到您的 index.php 代码,但请确保在 url 中或从提交的表单中手动设置变量“size”。如果您使用表单,请确保使用 method="GET"。如果您使用 method="POST",那么您的变量将在 $_POST['size'] 中。
Hope this helps.
希望这可以帮助。
回答by Guillaume Flandre
You may want to take a look at WP's documentation about the WP Query object.
您可能需要查看有关 WP Query 对象的 WP 文档。
http://codex.wordpress.org/Query_Overview
http://codex.wordpress.org/Query_Overview
It seems like wordpress uses the get_query_vars to method of the wp_query object to get queries.
似乎 wordpress 使用 get_query_vars to wp_query 对象的方法来获取查询。
You may also take a look at this one: http://codex.wordpress.org/Custom_Queries
你也可以看看这个:http: //codex.wordpress.org/Custom_Queries
回答by cdmckay
Try this:
尝试这个:
<?echo "hello world";?>
<?
if (array_key_exists('size', $_GET))
echo $_GET['size'];
?>
<?echo "end";?>
If you see
如果你看到
hello worldend
... that means you're not setting the size GET parameter. What URL are you using to access said page?
...这意味着您没有设置大小 GET 参数。您使用什么 URL 访问该页面?
回答by rbncha
Although it is quite old post it might be of good use to let know.... There is one plugin that can ease handling of $_GET, $_POST and global variable declaration issues pretty well. http://wordpress.org/extend/plugins/wordpress-registry/
虽然它是很老的帖子,但让我们知道它可能很有用.... 有一个插件可以很好地简化 $_GET、$_POST 和全局变量声明问题的处理。http://wordpress.org/extend/plugins/wordpress-registry/
回答by Aman Chhabra
May be this can somebody
可能是这可以有人
Please check your htaccess file. and have a rewrite rule there
请检查您的 htaccess 文件。并在那里有一个重写规则
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase <<relative_url>>/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . <<relative_url>>/index.php [L]
</IfModule>
# END WordPress
Please change << relative_url>> with the relative url as compared to your domain
与您的域相比,请使用相对 url 更改 << relative_url>>
I faced the same issue and my wordpress was installed on a godaddy server . But after changing the .htaccess file the issue got resloved.
我遇到了同样的问题,我的 wordpress 安装在 Godaddy 服务器上。但是在更改 .htaccess 文件后,问题得到了解决。

