因此,我对wordpress站点有以下条件:
if (is_page(array(1111, ???)) {
//do something for page# 1111 and url that contains "names"
}
我有一个页面的url如下:
example.com/names/steve
example.com/names/mike
所以,我想检查url是否是"名称"作为条件。
有人能告诉我如何更改条件以检查url中的"名称"吗?
感谢
编辑:
我换了下面的,但还没有运气。
if ((is_page(1111)) || (preg_match("/'/names$/", $_SERVER['REQUEST_URI'])))
or
if ((is_page(1111)) || (basename($_SERVER['REQUEST_URI']) == 'names'))
or
if ((is_page(1111)) || (strpos($url, 'names') !== false))
有什么建议吗?
使用正则表达式的preg_match()
函数:
<?php
$url = 'example.com/names/steve';
echo (preg_match("/names/i", $url)) ? 'Match' : 'No Match';
?>
举个例子:
if (is_page(1111) || preg_match("/'/names'//", $_SERVER['REQUEST_URI'])){
// your logic
}
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$myArray = explode("/", $link);
$page = end($myArray);
if($page == '1111') {
//Your code
}
编辑:如果URL包含尾部斜线(或不包含)
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$actual_link = rtrim('/', $link);
$myArray = explode("/", $actual_link);
$page = end($myArray);
if($page == '1111') {
//Your code
}