wordpress 在wordpress中获取查询字符串参数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11442180/
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
Getting querystring parameter value in wordpress
提问by rzb
I'm making a wordpress plugin. I've used add_query_string() inside anchors in order to load content based on what link the user has clicked. Now I need to know the best way to get the parameter value in the current URI.
我正在制作一个 wordpress 插件。我在锚点中使用了 add_query_string() 以便根据用户单击的链接加载内容。现在我需要知道在当前 URI 中获取参数值的最佳方法。
It's probably a pretty basic and stupid question, but I'm new to programming so I'm sorry if I misinterpret some terms.
这可能是一个非常基本和愚蠢的问题,但我是编程新手,所以如果我误解了一些术语,我很抱歉。
This is the code:
这是代码:
if ( current_user_can('manage_options') ) {
echo (
'<div>
<ul>
<li><a href="'.add_query_arg( 'adminoption', 1 ).'">option 1</a></li>
<li><a href="'.add_query_arg( 'adminoption', 2 ).'">option 2</a></li>
</ul>
</div>'
);
// if adminoption == 1 load content A
// if adminoption == 2 load content B
}
回答by David Carrus
I think you are asking for get_query_var()function. In your case you should use get_query_var('adminoption'). Hope it helps
我想你是在要求get_query_var()函数。在您的情况下,您应该使用 get_query_var('adminoption')。希望能帮助到你
回答by Reine Johansson
get_query_var('adminoption') only works with standard or registered vars. So for non-standard Wordpress vars you would need to register it first in your functions.php file:
get_query_var('adminoption') 仅适用于标准或注册变量。因此,对于非标准 Wordpress 变量,您需要先在您的 functions.php 文件中注册它:
function rj_add_query_vars_filter( $vars ){
$vars[] = "adminoption";
return $vars;
}
add_filter( 'query_vars', 'rj_add_query_vars_filter' );
get_query_var('adminoption');
Realize the question is old but hope it helps anyone.
意识到这个问题很老,但希望它可以帮助任何人。
回答by Taylored Web Sites
Raising hidden answer in the comments by David Carrus:
在 David Carrus 的评论中提出隐藏的答案:
Anyway you may try with the old php $_GET['adminoption'].
无论如何,您可以尝试使用旧的 php $_GET['adminoption']。
回答by Tomás Cot
To get a vars from the query string you can use PHP's $_GET['key']
method.
要从查询字符串中获取 vars,您可以使用 PHP 的$_GET['key']
方法。
Depending on what you are doing, you can also use get_query_var('key')
, this function works with parameters accepted by the WP_Query class (cat, author, etc).
根据您在做什么,您还可以使用get_query_var('key')
,此函数适用于 WP_Query 类(猫、作者等)接受的参数。
If you want to use custom query vars with this function, you need to use the query_varsfilter to modify the list of supported query vars, you can read how to do that in the documentation linked above.
如果你想在这个函数中使用自定义查询变量,你需要使用query_vars过滤器来修改支持的查询变量列表,你可以在上面链接的文档中阅读如何做到这一点。