[solved] How to find current page slug in theme

Hi there,

I’m working on theming an Omeka-S install and I need to know if the current page is the homepage or not. What is the best way of doing this?

Currently I have the following because the only way (that I can see) to work out which is the homepage is if its the first item in the navigation!? but then I’m not able to find the current page slug, only the current site slug.

    $linkedPages = $site->linkedPages();
    if ($linkedPages) {
        $firstPage = current($linkedPages);
        $currentPageSlug = '????';
        if($firstPage->slug() === $currentPageSlug) {}
   }

Can anyone point me in the right direction?

Hi,
I do not know if it is the best way to solve your problem, this works for me to access the slugs in theme:

$path =  $_SERVER["REQUEST_URI"];
$parts = explode('/', $path);

Then for a URL of the type server/myproject/s/mysite/page/homepage I have

$parts[0] = server
$parts[1] = myproject
$parts[2] = s
$parts[3] = mysite
$parts[4] = page
$parts[5] = homepage

I suspect there are more Zend-y ways to do it :slight_smile:

Thank @hajoki - I wanted to avoid doing that as much as possible since my theme is already quite hacky. I think I’ve worked it out now though!

How to find current page slug

$nav = $site->publicNav();
$container = $nav->getContainer();
$activePage = $nav->findActive($container);
$currentPage = $activePage['page']->getParams()['page-slug'];

How to find if the current page is the homepage (based on the logic that the homepage is the first one in the navigation.

<?php
    // @TODO make this a nice helpful helper
    $nav = $site->publicNav();
    $container = $nav->getContainer();
    $activePage = $nav->findActive($container);
    if (isset($activePage['page'])) {
        $currentPageSlug = $activePage['page']->getParams()['page-slug'];
        $linkedPages = $site->linkedPages();
        if ($linkedPages) {
            $firstPage = current($linkedPages);
            if ($firstPage->slug() === $currentPageSlug) {
                // this is the homepage
            }
        }
    }
    ?>
3 Likes

The current page slug should also be accessible as just $page->slug()

@jflatnes That was my first try but I get Notice: Undefined variable: page in /var/www/html/themes/le/view/layout/layout.phtml on line 30

I know this is an old post… the hackish solution proposed by @hndig above works for me, but I don’t seem to be able to get this working using the page->slug() call, as suggested above.

I suppose something like this should work:

<?php $pageSlug = $page->slug()?>

<? if ($pageSlug == "homepage"): ?>
<p> Something</p>
<? else: ?>
 <p> Something else</p> 
<? endif; ?>

but I get the following error:

Uncaught Error: Call to a member function slug() on null in /var/www/html/

If there is no better way I’ll probably stick with the solutions above, but something more elegant would be appreciated, as I suppose that having some custom features on the homepage is not so unusual.

I have not been able to find relevant references in the documentation.

Where (what file) are you trying to write this?

Similarly to @hndig above, in

view/layout/layout.phtml

inside my custom theme.

Thanks for reaching out!

Update:

the solution proposed by @hndig was actually breaking the “advanced search page”.

I mildly adapted @hajoki solution above, which is ultimately alright.

In brief, what I currently have is:

<?php $path = $_SERVER["REQUEST_URI"];?>

<? if ($path == "/s/website-slug/page/homepage"): ?>
<p> Something for the homepage</p>
<? else: ?>
<p>Something for all the rest</p> 
<? endif; ?>

Which is clear enough and does what I need.

1 Like

That’s a workable option… the issue you’re having with what I suggested is that it’s intended to go on the “page” view, omeka/site/page/show.phtml. That’s where the $page variable will be set (and also automatically sidesteps any issues of the markup getting applied on other things than user-created pages).

1 Like

Makes sense! thanks for clarifying!