0

How can I display an element in a specific page and all pages under that specific page using PHP? For example I want to display something under the page example.com/blog and all the subpages of /blog, like blog/another-page/ and blog/another-page/yet-another-page, but not in the homepage of that site.

In Magento, I was able to display a specific element on a specific page using the code below:

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();
$blogPaths = array('/blog', '/blog/', '/index.php/blog/');
if(in_array($path, $blogPaths))
{
    //Do something on /blog
}

SOURCE: Get Current URL in Magento and show something

But if I browse to /blog/another-page, that specific element that I want to show disappears. I also want to show it in all sub-pages of that sub-page.

The only method that I can think now is adding all the sub-pages in that array, but it will be very ver very long. For instance, I have 1000+ pages. That would be a very dumb move.

I have tried /blog/* but it didn't work. I'm pretty sure it's wrong. Is there a faster and easier way to check if it's a /blog page or any other page under that page, then show this blah blah blah?

I hope some Magento / PHP expert can guide me to the right path. :(

Community
  • 1
  • 1
jehzlau
  • 627
  • 2
  • 9
  • 20

1 Answers1

2

I guess you just added the '/blog/*' in the in_array function. That of course won't work. I would do the following:

function startsWith($haystack, $needle)
{
    return $needle === "" || strpos($haystack, $needle) === 0;
}

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();
if (startsWith($path, '/blog/') || (strcmp($path, '/blog') == 0))
{
    //Do something on /blog
}

Hope it helps (I got the starts with function from startsWith() and endsWith() functions in PHP)

Community
  • 1
  • 1
johnecon
  • 333
  • 2
  • 9
  • Yes I added * in the array, hahaha. Just a wild guess, but I'm pretty sure it's wrong, I just tried it. – jehzlau Aug 14 '14 at 06:36
  • Got an error that's saying, unexpected {. Hmmm.. Will figure this out, I think I'm close to a solution. O__O – jehzlau Aug 14 '14 at 06:42
  • I just added another ) in your code, now it runs without errors, but it doesn't work. It doesn't show anything even if I'm in the /blog page. Weird. O__O – jehzlau Aug 14 '14 at 06:59
  • I edited the code of the answer. Indeed I had forgotten one parenthesis. Can you try again? Can you try to debug it? What if you var_dump the output `$a = startsWith($path, '/blog/'); var_dump($a);` – johnecon Aug 14 '14 at 09:50
  • Wait will try that... :D – jehzlau Aug 14 '14 at 10:40
  • It says "bool(false)" on the page when I tried va_dump. Does it help? – jehzlau Aug 14 '14 at 10:42
  • Oh wait it now works. Amazing. What did oyu modify it your code above? I didn't notice any modification other than the ) – jehzlau Aug 14 '14 at 10:45
  • It now works like magic. Thank you so much! You've been a very great help. Now I can use this code in future projects. – jehzlau Aug 14 '14 at 10:47
  • just saw it in the revisions http://stackoverflow.com/posts/25298390/revisions. Wow! It's ) and (), and it worked. Hahaha! THanks again! Weeeeeeeeeeee! – jehzlau Aug 14 '14 at 10:48