I am working on a new site for an online publication From My Bottom Step and I wanted to be able to allow authors to display external links from the post and additional resources in the sidebar or the footer of the post like some major newspaper sites do. To do so I enlisted them help of a little used wordpress feature, custom fields.
The issue however was that I wanted it to be brain dead simple for authors to use. I didn’t want them to have to format the links with html and unordered lists, and that made it a bit tricky. So the final solution I came up with requires the author to use a new field for each link, but use the same key.
I called the key extlink, and used the code below to display the whole thing in one list. If there are no extlink fields selected then it wont display the block. If it does however it will display the header for the block, and the links below it.
< ?php $links = get_post_custom_values("extlink");
if ($links[0]!="") {
$mykey_values = get_post_custom_values('extlink');
echo "<div id='external-link-list'><h4 class='related'>External Links</h4>";
echo "<ul class='external-link-list'>";
foreach ( $mykey_values as $key => $value )
{
echo "<li><a href='$value'>$value</a></li>";
}
echo "</ul>";
}
?>
To customise this for your own key just go through and change extlink to whatever you chose for your key.













Baz L | August 3rd, 2008 at 10:45 am #
So where exactly does this code go?
Andrew Flusche | August 3rd, 2008 at 12:31 pm #
This is one of my favorite tips of yours. It certainly got a bookmark from me. Several times I’ve wanted to do something similar, but I never quite new how to make custom fields handle it. Now I do!!
Stephen Lee | August 8th, 2008 at 11:25 pm #
Great tip. I’m going to incorporate this in my blog. Thank you.