Blog Post Full URL - How?

  • Hi all. So I have a blog build where I have some social sharing buttons on the post.php page. I have them linked like:


    PHP
    1. <a href="https://twitter.com/home?status=<?php perch_blog_post_field(perch_get('s'), 'postTitle'); ?>%20-%20<?php perch_page_url(); ?>" class="fa fa-twitter"></a>

    This outputs the URL only up to the website.com/blog......it doesn't include the slug to complete the link. How do I do that outside a template?

  • Hi,


    The function perch_page_url() outputs the page URL as Perch sees it. You don't technically have a page for each blog post. A blog post is its own entity, and you display them dynamically.


    One option would be to build the URL manually. You can output the postURL with perch_blog_post_field():

    PHP
    1. $page_url = 'https://example.com' . perch_blog_post_field(perch_get('s'), 'postURL', true);


    Alternatively you can use the Pipit Sharing app (on GitHub) which helps you output sharing links in both PHP and Perch templates:


    PHP
    1. <?php $postTitle = perch_blog_post_field(perch_get('s'), 'postTitle', true); ?>
    2. <a href="<?php pipit_sharing_link('twitter', ['desc' => $postTitle]); ?>" target="_blank" rel="noopener">
    3. Share on Twitter
    4. </a>


    HTML
    1. <a href="<perch:sharing id="twitter" desc="{postTitle}">" target="_blank" rel="noopener">
    2. Share on Twitter
    3. </a>
  • The first option is not outputting anything

    The example I shared assigns the value to a variable; it does not output it. You can echo the value:


    PHP
    1. $page_url = 'https://example.com' . perch_blog_post_field(perch_get('s'), 'postURL', true);
    2. echo $page_url;


    Or use it where you need it. So if you were to build the URL manually in PHP, you could use something like:


    PHP
    1. <?php
    2. $post_title = perch_blog_post_field(perch_get('s'), 'postTitle', true);
    3. $page_url = 'https://example.com' . perch_blog_post_field(perch_get('s'), 'postURL', true);
    4. $twitter_url = 'https://twitter.com/intent/tweet?url=' . urlencode($page_url) . '&text=' . urlencode($post_title);
    5. ?>
    6. <a href="<?= $twitter_url ?>">
    7. Share on Twitter
    8. </a>