Forum
Passing Author Collection Relationship fields into Page Attributes for Articles
I have two collections one for Blog and one for Authors. In my post's meta data I want to show the article:author meta but as it is relationship data it wont pull through like it does with the blog pots's collection data. Is it possible to do? I'm using the code below:
attributes file contains
<meta property="article:author" content="<perch:pages id="og_author" type="hidden" />" />
post.php contains
<?php
# get the post
$post = perch_collection('Blog', [
'filter' => 'titleSlug',
'match' => 'eq',
'value' => perch_get('s'),
'skip-template' => 'true',
'return-html' => 'true',
]);
# set up the variables
$title = $post['0']['heading'];
$description = strip_tags($post['0']['desc']);
$keywords = strip_tags($post['0']['keywords']);
$noindex = strip_tags($post['0']['noindex']);
$nofollow = strip_tags($post['0']['nofollow']);
$nosnippet = strip_tags($post['0']['nosnippet']);
$authorfirst = strip_tags($post['0']['author_first_name']);
$authorlast = strip_tags($post['0']['author_last_name']);
if (isset($post[0]['postImageSocial'])) {
$socialImage = $post[0]['postImageSocial'];
} else {
$socialImage = '';
}
# use the variables in the array value
perch_page_attributes_extend(array(
'description' => $description,
'keywords' => $keywords,
'noindex' => $noindex,
'nofollow' => $nofollow,
'nosnippet' => $nosnippet,
'sitename' => 'CPI',
'og_description' => $description,
'og_title' => $title,
'og_author' => $authorfirst . ' ' . $authorlast,
'og_type' => 'article',
'og_image' => $socialImage,
'sharing_image' => $socialImage,
));
?>
Instead of
$post['0']
, try$post[0]
Hi Drew, I'm afraid that didn't work.
What does
print_r($post)
give you?It outputs the following on the front end:
So no
author_first_name
and noauthor_last_name
. Where would you expect those to be?They are on the post.php page wrapped in the relationship template tags. It is correctly outputting the information on the front end using the following:
The only reference to author in the print out is the following - which is where the first and last name is represented in the front end template.
Ah, ok, I've got you. So those are the IDs of the related items in the Authors collection.
might get you there.
Thanks, where should I be using that line?
That's an example - not a literal instruction :)
Thanks Drew - a great help once again!
Full solution for anyone else trying to do this: