0

我正在使用重定向到随机帖子的 wordpress 插件。它允许我根据标签重定向到随机帖子,所以网址可能看起来像

example.com/?random&random_tag_id=100

如果我想找到带有标签 id 100 和 101 的随机帖子,我会这样做

example.com/?random&random_tag_id=100&random_tag_id=101

但我想找到来自 ids 100 或 101 的随机帖子。我知道 & 用于“this + this”,但是否可以通过 URL 发出“this OR this”请求?

谢谢你的帮助!

4

4 回答 4

1

你可以这样做:

example.com/?random&random_tag_id=100,101,102,103

然后,当您处理这些变量时,您必须执行以下操作:

<?php
$random_tag_ids = explode(',', $_GET['random_tag_id']);
// $random_tag_ids now contains an array of your random tag ids
?>
于 2013-10-08T20:15:50.903 回答
0

You're misunderstanding how GET parameters work.

In a Query String (?key=a&otherkey=b), the & is just a delimiter between the different key/var pairs. So the PHP equivalent of that string is this:

$_GET['key'] = 'a';
$_GET['otherkey'] = 'b';

If you use the same key twice in a query string, the latter will overwrite the former. So for the string ?key=a&key=b, $_GET['key'] will be equal to b.

As others have said, you can pass data via a comma separated list, (i.e. ?key=1,2,3,4), or via an array, (i.e. ?key[]=a&key[]=b).

于 2013-10-08T20:43:19.997 回答
0

You can pass arrays to query like this:

example.com/?random&random_tag_id[]=100&random_tag_id[]=101

You'll get $_GET['random_tag_id'] == array(100, 101)

于 2013-10-08T20:18:25.810 回答
0

The problem with doing it that way is you'll overwrite the value in PHP. You need bracket notation to pass it correctly as an array

example.com/?random&random_tag_id[]=100&random_tag_id[]=101

When you get to your PHP then you'll have

echo $_GET['random_tag_id'][0]; //Outputs 100

You could then pass into your query an imploded list

于 2013-10-08T20:18:49.673 回答