我有一个页面,我想在其中输出一个a
元素,该元素在特定情况下通过添加GET变量链接到当前页面。这是我的代码:
<?php
$Path=$_SERVER['REQUEST_URI'];
$URI=home_url( ).$Path;
if($_GET['showall']==1)
{
$URI = strtok($URI, '?'); //This removes the GET variables
$showWhat = "Show 12 per Page";
}
else
{
$URI .= '?' . http_build_query(array('showall'=>1));
$showWhat="Show All";
}
?>
因此,如果showall
为1,它只返回当前页面的URL,末尾没有showall
变量。如果它不是1,那么它会将?showall=1
附加到URL的末尾。
如果URL中还没有另一个GET变量,这将非常有效。如果有,我会得到这样的URL:
http://example.com?orderby=price?showall=1
这显然不起作用,因为变量之间缺少&
。
如果已经存在GET变量,我如何修改代码以使其工作。
请考虑,如果showall=1,那么我希望输出带有除showall之外的其他GET变量的URL。例如http://example.com?orderby=price?
。我相信只要使用我使用过的就会去掉所有的GET变量
(这是一个wordpress网站,因此使用home_url()
(
怎么样:
$URI .= '?' . http_build_query(array_merge($_GET,array('showall'=>1)));
L。E:也许:
if($_GET['showall']==1) {
$get = $_GET;
unset($get['showall']);//since you don't want this anymore
$URI = home_url('your-path-here') . '?'. http_build_query($get);
}
请注意,home_url()
可以采用uri参数,因此将相对url传递给home_url((函数(即:home_url('plugins/superplugin/the-page.php')
(。