Adding variable for post title so post template can include auto-generated link to view on external website.com/{post title}

There’s an external website that I’ll be referencing within my posts that uses the same static format for their pages. Let’s say it’s website.com/view/1111, website.com/view/1112, etc.

What I want to do is create a post template that will automatically append a link to that external site above the post content. So, for example, if my post was titled 1337, the post would automatically include a link at the top “View on website.com” that would point to website.com/view/1337.

Alternatively, if that’s not feasible to include in a template file, it’d be fine if it were just a shortcode I could use at the beginning of posts.

I tried adding <?php echo “<a href="https://wordpress.stackexchange.com/questions/372492/website.com/view/<?php the_title(); ?>’>View on website.com</a>” ; ?> in my template and that broke the site, so, guessing I did not do that properly.

1 Answer
1

OH I figured it out.

     <?php $myvariable = get_the_title( "'. '', false );
        echo "<a href="https://wordpress.stackexchange.com/questions/372492/website.com/view/$myvariable">View on website.com</a>";
        ?>

Hopefully this helps someone else!

Leave a Comment