问题
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.
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/8297991/wordpress-how-to-check-if-the-slug-contains-a-specific-word