Forum

Thread tagged as: Question, Runway

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        = 'http://'.$_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');
?>
Dan Lee

Dan Lee 1 points

  • 3 years ago
Drew McLellan

Drew McLellan 2636 points
Perch Support

What result are you getting?

Dan Lee

Dan Lee 1 points

I get nothing - the url doesn't get output.

Drew McLellan

Drew McLellan 2636 points
Perch Support

Have you checked the value of $post ?

Dan Lee

Dan Lee 1 points

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".

<?php 
print_r(PerchSystem::get_vars());
?>

Output

Array ( [url_0] => /blog/how-valuable-is-our-food-waste [url_s] => how-valuable-is-our-food-waste [url_1] => how-valuable-is-our-food-waste [perch_page_path] => /blog [domain] => http://armstrong.dev [url] => http://armstrong.dev/blog/how-valuable-is-our-food-waste [sharing_image] => /assets/img/icons/default_fb_image.jpg [twittername] => @ukCPI )

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.

<perch:content id="url" type="hidden" />
Drew McLellan

Drew McLellan 2636 points
Perch Support

Try:

<perch:content id="url_0" type="hidden" />

as that matches the data you have.

Dan Lee

Dan Lee 1 points

Yes that does outputs the part of the URL but really after the full URL.

Drew McLellan

Drew McLellan 2636 points
Perch Support

It should output the part that changes, right? Isn't the rest always the same?

Dan Lee

Dan Lee 1 points

I could hard code the first part but then it is different on my dev, staging and live versions.

Drew McLellan

Drew McLellan 2636 points
Perch Support

In which case best to pass it into the template from an environment variable.