Get Current Page Name

Permalink 3 users found helpful
I'd like to get the current page name so I can put it at the top/header of my left hand side secondary navigation column. I'm not looking for breadcrumbs, just need to show the current page name.

I've been looking through the readily available information on themes and also looking at the code in the default themes but I don't see this anywhere. Anyone have a clue how to do it?

View Replies: View Best Answer
ryan replied on at Permalink Best Answer Reply
this should do it.
$page = Page::getCurrentPage();
echo $page->getCollectionName();
webjedi replied on at Permalink Reply
Actually, Ryan, that didn't work (might be older version).

The code that I got to work is:

echo $c->getCollectionName();

This works fine when outside of the Area object ($a). I am trying to fetch the stored value of an area object before displaying it. ie. NOT $a->display($c)

This post helped me find that. Thanks :)

jshannon replied on at Permalink Reply
For those finding this, the first way (Page::getCurrentPage();) is actually the "right" way to do it. You should avoid the use of the global $c variable whenever possible.

Not sure it didn't work, but if anybody has any problems with the first method post here and I'll assist.

eNViSiOn replied on at Permalink Reply
Could you explain why we should avoid using the variable $c?
adrenalin replied on at Permalink Reply
Already one year old message, but nevertheless... It is better to use a method, because the context might not have that variable `$c` defined. It is NOT automatically a global variable, but it might be context dependent.

Second issue of using a variable is that it might be overwritten (using something as ubiquitous as a letter `c` for a (pseudo) global variable is already giving me the creeps), but a method will never be overwritten in PHP.