6

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.

totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
Andy
  • 454
  • 2
  • 7
  • 22
  • Why don't you use categories for that? I mean the slug is controlled by the page title or whatever you specify the slug to be. So why use the `front-end` (the slug) instead of the way more accessible `back-end` (category/page-name)? Also to get decent answers, ask a **real** question! What did you try yet, do you want to know whether it's possible, do you want code or whatever.. – Anonymous Nov 28 '11 at 15:25
  • also what language do you want to do it in? I answered with a PHP example but javascript can do this too. – totallyNotLizards Nov 28 '11 at 15:28
  • I am using categories, i am also using a plugin to run two 'index' pages, so I can't use category-slug.php templates or any such things. That is why I phrased the question in more general terms. Thanks. – Andy Nov 28 '11 at 15:59

2 Answers2

20

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

totallyNotLizards
  • 8,489
  • 9
  • 51
  • 85
  • 1
    if tests might seem odd, however according to the manual page: "This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function." – totallyNotLizards Nov 28 '11 at 15:27
0
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

Liam Stewart
  • 209
  • 2
  • 10