Forum

Thread tagged as: Question, Runway

Pulling the page slug

I have a page called "Save Money" I want to pull the slug of "save-money" back into my php page. Is there a perch tag or php tag that will pull that slug in based off of the page name and or nav text?

Brandon Livengood

Brandon Livengood 1 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you should be able to use:

 perch_page_attribute('pagePath');

that outputs /save-money

Drew McLellan

Drew McLellan 2638 points
Perch Support

Great.

How can I remove the / to just get the page slug?

I can use replace=/| if I use it in a perch template file.

Sorry if i'm intruding but you can do it just in php if you can't do it in Perch:

<?php
    $url = basename($_SERVER['REQUEST_URI']);
    $keys = parse_url($url); // parse url
    $path = explode("/", $keys['path']); // split path
    $slug = end($path); // slug
?>

Then just echo $slug where you need it

Drew McLellan

Drew McLellan 2638 points
Perch Support

Brandon, yes, you can do this:

echo ltrim(perch_page_attribute('pagePath', [], true), '/');

Great, that works...

Any plans for have a pageSlug that just pulls the actual page slug in the future?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sure, I'll add something.

Fantastic :)