Forum

Thread tagged as: Question, Problem

Retrieve page title when using perch_content_custom

Hi. I'm using perch_content_custom to display truncated content from a set of pages elsewhere on the site. Is there a way, within that loop, to retrieve the page title for each page?

Here's the code that produces the loop:

<?php
    perch_content_custom('Service', array(
        'page'=>'/services/**/*',
        'template'=>'service_abstract.html'
    ));
?>

This works well, looping through each /services/ page and displaying the content from each 'Service' region. But I'd also like to display the page title. Is there a way to do this? Thanks in advance :)

Will Wallace

Will Wallace 0 points

  • 4 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

I don't know if I'm over-complicating this or not, but:

I think you'll have to use the each option

perch_content_custom('Service', array( 
'page'=>'/services/**/*', 
'template'=>'service_abstract.html',
'each' => function($item) {
        // process as necessary, then return the modified item
        return $item;
    },
));

Within that function (the "processing" bit), I think you would have to get the $item['pageID'] and then pass that into:

$item['page_title'] = perch_page_attribute('pageTitle', array(
        '_id' => $item['pageID'],
    ), true);

(So, you're getting the pageID from each loop, passing it into the perch_page_attribute function and then adding a new field to each item).

You can call that new field in your template by using <perch:content id="page_title" />

Untested. I think it's right though.

Thanks!

$item['pageID'] is missing an underscore - it should be $item['_pageID'] - but other than that it works perfectly!

Duncan Revell

Duncan Revell 78 points
Registered Developer

Excellent!