wordpress: how to check if the slug contains a specific word?

Andy picture Andy · Nov 28, 2011 · Viewed 16.6k times · Source

I'm trying to use a conditional statement to check if either of two words (blog & news) appear in the slug. If one shows up I will display one menu, if the other then its corresponding menu shows.

Answer

totallyNotLizards picture totallyNotLizards · Nov 28, 2011

Using PHP:

$url = $_SERVER["REQUEST_URI"];

$isItBlog = strpos($url, 'blog');
$isItNews = strpos($url, 'news');

if ($isItBlog!==false)
{
    //url contains 'blog'
}
if ($isItNews!==false)
{
    //url contains 'news'
}

http://www.php.net/manual/en/function.strpos.php