Forum

Thread tagged as: Problem

Perch_page_attribute to Body class variable

Hi

I'm using

perch_page_attribute('pageTitle'); 

which gets the page title, but I want to pass it into a variable.

$page_title = perch_page_attribute('pageTitle', true);  

which still echoes the page title into the page's html, not passing it into the variable. This ',true' feature is common across other page functions in perch. Is it not available in perch_page_attribute?

I then go on to use this php custom function to turn it into a slug:

function to_slug($string){
    return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
        }

Is this possible with the slug feature of perch:pages?

Thanks

Adam Menczykowski

Adam Menczykowski 1 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Should be:

$page_title = perch_page_attribute('pageTitle', [], true);

You can use PerchUtil::urlify() to create a slug.

Great thanks Drew,

my final code (before I get the time to play with PerchUtil::urlify()...

function to_slug($string){
    return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
        }
        // Input: This is My Title
        // Returns: this-is-my-title 
    $page_title = perch_page_attribute('pageTitle', [], true);  
    $page_class = to_slug($page_title);
    ?>
    <body class="<?php echo $page_class; ?>">