Forum
Collection Item (Page) URL as a Variable for Share Buttons
Hi, I have set up my url as a variable to pass into the attributes and it works great for my collection items (I can see it in the source for the meta data). The following code lives in my header.php file:
<?php
$domain = 'https://'.$_SERVER["HTTP_HOST"];
$url = $domain.$_SERVER["REQUEST_URI"];
PerchSystem::set_var('domain',$domain);
PerchSystem::set_var('url',$url);
?>
What I want to do now is use that variable value for my share button links in the collection item. I add the following into the template file but it doesn't show the url for the collection item's page:
<perch:content id="url" type="hidden" />
Here is the full code for displaying the collection item and getting content into the meta data (post.php):
<?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 = $post['0']['desc'];
$keywords = $post['0']['keywords'];
$noindex = $post['0']['noindex'];
$nofollow = $post['0']['nofollow'];
$nosnippet = $post['0']['nosnippet'];
if (isset($post[0]['postImageSocial'])) {
$socialImage = $post[0]['postImageSocial'];
} else {
$socialImage = '';
}
if (isset($post[0]['author']) && is_array($post[0]['author'])) {
foreach($post[0]['author'] as $authorID) {
$author = perch_collection('Authors', [
'_id'=>$authorID,
'skip-template' => true,
]);
// do whatever you will with the $author.
$authorfirst = $author['0']['author_first_name'];
$authorlast = $author['0']['author_last_name'];
}
}
# 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,
'url' => $url,
));
?>
<?php
// Include the header. You can find this in tempates/layouts/global
perch_layout('global/header', [
'title' => $title,
'body-class' => 'blog-post',
]);
?>
<?php
# display the blog post
echo $post['html'];
?>
<?php
// Include the footer. You can find this in tempates/layouts/global
perch_layout('global/footer');
?>
What result are you getting?
I get nothing - the url doesn't get output.
Have you checked the value of
$post
?Yes - I used print_r($post) - it doesn't show the value of id="url". However I use the following to get all variables and I can see that it does show the value for "url".
Output
However using the following in the template doesn't print out the value of the variable so I'm a bit stumped. Like I said above I have used it in the attributes template without problem.
Try:
as that matches the data you have.
Yes that does outputs the part of the URL but really after the full URL.
It should output the part that changes, right? Isn't the rest always the same?
I could hard code the first part but then it is different on my dev, staging and live versions.
In which case best to pass it into the template from an environment variable.