2015-11-04 51 views
1

所以,我有以下條件WordPress站點:檢查的URL,如果條件

if (is_page(array(1111, ???)) { 
//do something for page# 1111 and url that contains "names" 
} 

我有了網址如下頁面:

example.com/names/steve 
example.com/names/mike 

所以,我想檢查該網址是「名稱」作爲條件。

有人能告訴我如何更改條件來檢查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)) 

有什麼建議嗎?

+0

'strpos($ url,'names') !==假' –

+0

您使用不需要的括號 –

回答

2

隨着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 
} 
+0

謝謝。有效。學到了新東西。 =) –

+0

您好,我很高興它有幫助 –

2
$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 
}