简体   繁体   English

猫的WP_Query,其中cat来自$ _GET []返回所有帖子

[英]WP_Query by cat where cat comes from $_GET[] returns all post

I am trying to get all post of a cetegory throught a WP_Query: 我试图通过WP_Query获取所有的cetegory帖子:

//url whould be www.blablabla.com/?cat=4
$featured = new WP_Query('cat=$_GET["cat"]');

where cat is the Http GET param. 其中cat是Http GET param。 This retuns all the post ignoring the "cat" parameter. 这会重新调整所有忽略“cat”参数的帖子。 But if I write an integer value instead of the $_GET I get the expected post, ie: 但是如果我写一个整数值而不是$ _GET我得到了预期的帖子,即:

//This returns all the post of the category with slug = 4
$featured = new WP_Query('cat=4');

If I write $_GET["cat"] before then a 4 appears on screen. 如果我之前写了$ _GET [“cat”],屏幕上会出现4。 I supposse That WP_Query is getting the param as string and this is making the condition to be ignored, but I have try to cast in anyway the param but I can't get it to work. 我认为WP_Query正在将param作为字符串,这使得条件被忽略,但我仍试图抛出参数,但我无法让它工作。 I am new to Wordpress so I am sure this is pretty much stupid. 我是Wordpress的新手,所以我确信这非常愚蠢。

Thanks in advance! 提前致谢!

Try 尝试

$featured = new WP_Query('cat='.$_GET["cat"]);

The parameter you're passing to WP_Query is a single-quoted string. 您传递给WP_Query的参数是单引号字符串。 No variable interpolation is done on single-quoted strings, so you're actually passing the literal string cat=$_GET["cat"], when you want to pass cat=4 没有对单引号字符串进行变量插值,因此当你想传递cat = 4时,你实际上传递了文字字符串cat = $ _ GET [“cat”]

Are you sure $_GET["cat"] is giving you the expected value? 你确定$ _GET [“cat”]给你预期值吗?

try: $cat = $_GET["cat"];//You should add validation here var_dump($cat);//just to check to see that the right value is there, remove this line when done $featured = new WP_Query("cat=$cat"); 尝试:$ cat = $ _GET [“cat”]; //你应该在这里添加验证var_dump($ cat); //只是为了检查是否存在正确的值,完成后删除此行$ feature = new WP_Query ( “猫= $猫”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM