wordpress: how to check if the slug contains a spe

2019-04-08 15:55发布

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.

2条回答
Ridiculous、
2楼-- · 2019-04-08 16:38
slugContainsBlog = "blog"
if(window.location.href.indexOf(slugContainsBlog) > -1) {
   //Contains Blog
}

slugContainsNews = "news"
if(window.location.href.indexOf(slugContainsNews) > -1) {
   //Contains News
}

Here is a javascript version.

You can learn more about indexOf here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

查看更多
Root(大扎)
3楼-- · 2019-04-08 16:57

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

查看更多
登录 后发表回答