Hey there, I'm Connor. This is my blog.

How to link to external posts in octopress

Posted on 09 Jan 2012

Earlier this week, Jonathan Protsky tweeted at me, asking how I link to external posts in my custom Octopress theme. I figured Jonathan isn’t the only one wondering how to do this, and since it isn’t exactly obvious, I’m here to share how I went about implementing that functionality.

It’s all in the YAML

As you can see here, Octopress allows any variable(s) to be added to the YAML of your post. The way I went about doing it was by adding an external-url attribute.

For example, see the post below

As you can see, I’m simply adding an external-url attribute, and if I want to link to a post that is at a different URL, I just put the URL in there.

Now, you may have noticed that I also gave the post some content. That’s becuase with my custom theme, I have links to the previous/next posts after the current post. If one of those links happens to be a post that uses an external-url, I’d like to give them some context on my own site before sending them off to another one.

Okay, it’s not all in the YAML

There’s actually one more step. We just have to adjust the index page that loops through and lists all of your posts (most likely source/index.html).

All you need to do is check to see if post.external-url exists. See the example below (that’s actually the index.html file I use):

And, as you can see, I’m prefixing all of my posts that link to an external URL with [link], so the user knows what to expect. I’d recommend giving some kind of visual cue to the user.

← Play all rdio songs on mobile... kind of. HTML5...plz? →

Hey there, I'm Connor. You can find me on github, twitter, rdio, linkedin, facebook, and gmail. I helped prototype Lift and I'm excited to work for Apple this summer in Cupertino. I'm a junior studying Computer Science at SLU. This site is proudly powered by octopress.