Forum

Thread tagged as: Question, Runway

Get Parent page title based on subpage pagePath or pageID

I have a php page using perch_content_custom to display subpage content in an overlay via ajax call.

How can I get the pages Parent title?

I was hoping I could get the parent page info by passing the subpage pagePath or pageID to a function

Thanks

Keith Winter

Keith Winter 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can get the page title with perch_page_attribute('pageTitle'), which will also accept a pageID:

perch_page_attribute('pageTitle', [
    '_id' => $pageID,
]);

Thanks Drew. It is the parent page Title I need but the parentPage ID is not present in the sub page content array? How can I get the parent page ID when I am pulling in subpage content via ajax?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, you should have the parent ID available in the details for the page.

Hi Drew I was hoping you'd say that however I'm not getting the parent ID -

$subpageContent = perch_content_custom('Overlay Subpage', [
        'page' => $thepath,
        'template'   => 'overlay_subpage.html',
        'skip-template' => true,
        'return-html' => false,
    ]);

Gives me var_dump -


array (size=1) 0 => array (size=21) '_id' => string '55' (length=2) 'heading' => string 'text here' (length=13) '_title' => string 'text here' (length=13) 'slug' => string 'text here' (length=13) 'page-image' => string 'text here' (length=41) 'alt' => string 'text here' (length=8) 'panel-text' => string 'text here' (length=49) 'subhead' => string 'text here' (length=49) 'TextLeft' => string 'text here' (length=452) 'TextRight' => string 'text here'... (length=768) 'Highlight' => string 'text here' (length=95) 'stat-header-left' => string 'text here' (length=11) 'stat-text-left' => string 'text here' (length=16) 'stat-header-right' => string 'text here' (length=12) 'stat-text-right' => string 'text here' (length=17) 'footer-text' => string 'text here' (length=142) 'footer-link' => string 'text here' (length=31) 'footer-btn' => string 'text here' (length=25) '_page' => string 'text here' (length=31) '_pageID' => string '30' (length=2) '_sortvalue' => string '1000' (length=4)

Am I missing something here? I'm not getting the parentID is this array.

cheers Keith

Hi Drew I assume I'm not getting the parentID as I'm only getting content not current page details?

Subpage content is loaded by ajax into overlay after the actual page is loaded. Next and Prev links then run ajax call to get next or previous subpage which is loaded into existing overlay without changing the parent web page. Hope that makes some sense!

Would I therefore need to use PHP to run connect to perch db and run custom mysql query eg. something like select pageParentID where pageID = 123

Or is there a perch function that would return the pageParentID where pageID = 123

Thanks

Keith

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you tried using the navigation functions? Those give you the page information, not the content functions.

Ah ok I'll take a look at that. Thanks

Ok I've nearly got it but...

This returns an array with page details including pageParentID which is good.

$page = perch_pages_navigation(array(
        'from-path' => $thepath,
        'levels'    => 1,
        'include-parent'       => true,
        'skip-template'        => true,
    ));

This outputs to the screen the pageParentTitle

perch_page_attribute('pageTitle', [
        '_id' => $page[0]['pageParentID'],
        ]);

I can't seem to create a variable $theParentTitle so I can echo it out in page -

I would like to do something like

$theParentTitle = perch_page_attribute('pageTitle', [
        '_id' => $page[0]['pageParentID'],
        'skip-template'        => true,
        ]);

Then

echo $theParentTitle;

Hope that makes some sense.

Keith

Duncan Revell

Duncan Revell 78 points
Registered Developer

Almost there...

$theParentTitle = perch_page_attribute('pageTitle', [ '_id' => $page[0]['pageParentID'] ], true);

should work.

The , true as the last parameter of the function returns the value instead of echoing.

Thanks Duncan that's got it. Keith