Forum

Thread tagged as: Question, Problem

Get site URL inside templates for share buttons

Is there a way to get the site URL inside a template for share buttons as twitter, facebook etc.? Found a way with php but dont seem like i can use php with templates?

Thanks in advance

Erick Green

Erick Green 0 points

  • 6 years ago

You can pass variables into templates, but it will not work with perch_content(). You would have to use perch_content_custom().

Drew McLellan

Drew McLellan 2638 points
Perch Support

Or use:

<perch:setting id="siteURL" />

Already tried that and it works, but i need to collect the slub as well. I have a created a list detail with undersites and slub from title. Is this possible?

Drew McLellan said:

Or use:

<perch:setting id="siteURL" />

I also found this snippet: <?php perch_page_url(); ?>. Obviously i need the template to be php instead of html, is that possible as well?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, use PerchSystem::set_var() to pass it into the template.

How would you set up this if its the <?php perch_page_url(); ?> information i need to get into my template?

Thanks in advance

<?php PerchSystem::set_var('var_url', perch_page_url()); ?>

Then in template...

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

But you have to use perch_content_custom()

I already got this.

<?php
        perch_content_custom('Projects', array(
          'page' => '/strategy.php',
          'template' => 'projects-template.html',
          'filter' => 'slug',
          'match' => 'eq',
          'value' => perch_get('s'),
          'count' => 1,
        ));
        ?>

Then all you need to do is follow the above and it will be working...

:)

Is this correct? Cant get it to work. I only get the domain name not the whole url. I get: https://mydomain.com/perch_page_url/

The url i need is https://mydomain.com/projects.php?s=projectname

<?php
        PerchSystem::set_var('var_url', perch_page_url);
        perch_content_custom('Projects', array(
          'page' => '/strategy.php',
          'template' => 'projects-template.html',
          'filter' => 'slug',
          'match' => 'eq',
          'value' => perch_get('s'),
          'count' => 1,
        ));
        ?>

Then you need to append the slug (s) to the url in your template. The "url" is simply the url. If you need additional quarry information appended then you have to append this in your template.

something like this...

<a href="<perch:content id="var_url" type="hidden" />?s=<perch:content id="slug" />">link</a>

Since I have never seen your actual template I have to generalize some of this, but enough is here to give a good idea of how this works.

:)

Yes, but still missing the projects.php in between.

<a href="/<perch:content id="var_url" type="hidden"/>?s=<perch:content id="slug" type="hidden"/>">Share</a>

With this code i get: https://mydomain.com/perch_page_url?s=projectname

What i need to get: https://mydomain.com/projects.php?s=projectname

Is my perch_page_url code wrong somewhere? Tried to replace with projects.php but dont like .'s very much. Not an php expert as you probably already noticed. Really appreciate all the help.

Is the perch content custom tag already on project.php? If yes, then all you need is a relative URL rather then the domain, file, stringed query.

Providing a complete template as well as the runtime function is very important in the beginning. Bits and pieces of code and templates makes solving issues like this very difficult.

I am glad to help but as much information as possible really helps.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Instead of:

PerchSystem::set_var('var_url', perch_page_url);

You need

PerchSystem::set_var('var_url', perch_page_url([], true));

Actually i ended up with only getting the slug but the other way worked as well. Thanks!

Drew McLellan

Drew McLellan 2638 points
Perch Support

If that's a new problem, please raise a new thread. This one is marked as solved, so we won't be able to try an new issue here.