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

荒凉一梦 提交于 2020-02-20 08:39:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!