Jump to content

Including PHP variable in <a href> -- please help!


Recommended Posts

Hi there,

 

I am trying to have a PHP variable in my link, so that my final link would look like this:

 

http://www.example.com/eformat/variable-goes-here.rtf

 

I've managed to get http://www.example.com/variable-goes-here using the code below, but I can't seem to insert the "eformat/" and ".rtf" parts. I really don't know PHP very well, so help would be greatly appreciated. Please excuse my beginner level. Thank you!

 

<?php $eformat = $post–>post_name; echo "<a href=\"$eformat\">Download this blog post in eFormat (.rtf)</a>" ?>

Link to comment
https://forums.phpfreaks.com/topic/223023-including-php-variable-in-please-help/
Share on other sites

Karim, your code only gave me http://www.example.com/rtf. What do you think?

 

Maq, I implemented your code but it only gave me http://www.example.com/eformat/.rtf. I'm not sure why it refuses to recognize the variable $eformat, since the code in my first post makes it work... any ideas?

 

<?php $eformat = $post–>post_name; echo "<a href='eformat/{$eformat}.rtf'>Link</a>" ?>

It is variable that returns the post slug in Wordpress. So for example, a blog post that has the URL http://www.example.com/example-post would return as "example-post" (without quotes) when using that variable. Same thing with http://www.example.com/2010/12/example-post.

 

My purpose is to make the blog post downloadable in rtf format. I want to have a direct link to the .rtf file of each blog post. I figure that if I make a folder located at http://www.example.com/eformat and put .rtf files with the same names as the post slugs of the blog posts, it would work. I could try an alternative variable, such as the post title or the post number...

 

EDIT: Here is the list of the variables I can use. I would still prefer to use the post slug though. Makes things simpler and better for the reader, than something called 001.rtf.

 

I'm assuming I can't just use $post–>post_name since it always gives me errors... that's why I switched to using $eformat. But like I said, I virtually know no PHP, so I'm not really knowledgeable on what works and what doesn't, what code is correct and what is incorrect.

Wow, two solutions have been found on two other forums. :D And here I was trying to figure out even one solution by myself.

 

<?php echo "<a href='eformat/{$post->post_name}.rtf'>Download this blog post in eFormat (.rtf)</a>" ?>

 

<?php $var = $post->post_name; echo '<a href="eformat/' . $var . '.rtf">Download this blog post in eFormat (.rtf)</a>'; ?>

 

Maq, thank you for your help. Your code was essentially the same as the solution; it was just me who thought I couldn't use plain old $post–>post_name with it. :D

 

Thanks everyone!

 

Sorry, I meant the title attribute, as in <a href="" title="">. Following your example though, I've devised this:

 

<?php echo "<a href='eformat/{$post->post_name}.rtf' title='{$post->post_title} (.rtf)'>(.rtf)</a>" ?>

It works perfectly; thanks again! =D

 

Which solution (the 'var' one or this one) would be considered a more clean code? Which one should I use? I like the one above since it essentially takes up less space, but perhaps you consider the other to be more organized and easy-to-read?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.