
Sometimes, you may want to publish a post which only consist of an url pointing to some external resource. As there’s no built-in function to do that in WordPress, let’s create our own!

Sometimes, you may want to publish a post which only consist of an url pointing to some external resource. As there’s no built-in function to do that in WordPress, let’s create our own!
The first thing to do is to open your functions.php file and paste the following code:
function print_post_title() {
global $post;
$thePostID = $post->ID;
$post_id = get_post($thePostID);
$title = $post_id->post_title;
$perm = get_permalink($post_id);
$post_keys = array(); $post_val = array();
$post_keys = get_post_custom_keys($thePostID);
if (!empty($post_keys)) {
foreach ($post_keys as $pkey) {
if ($pkey=='url1' || $pkey=='title_url' || $pkey=='url_title') {
$post_val = get_post_custom_values($pkey);
}
}
if (empty($post_val)) {
$link = $perm;
} else {
$link = $post_val[0];
}
} else {
$link = $perm;
}
echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
}
Once done, open your index.php file and replace the standard code for printing titles:
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
by a call to our newly created print_post_title() function:
<?php print_post_title() ?>
Now, whenever you feel like let your post title leave your blog and point someplace else, just scroll down in your post writing screen, create or select a custom key named url1 or title_url or url_title and put that external URL in value box.
Thanks to Vlad Grubman for this awesome recipe!
20 Responses
But we can do that by simply giving urls in post right??? if my guess was wrong then i apologize..
Is there room in this function for styling external linking titles differently?
Vinoth: the URL goes in a Custom Field in each post, not in the post body.
I think there’s a plugin that allows you to define a page that links to an arbitrary URL. Probably does the same thing as this custom function.
This is a great hack. Can you help me with the code to take that URL and echo it somewhere in the post. I’ve tried it several ways and can’t get it to work. Basically, the problem is if someone comes to the post directly they won’t see the “Read More” link and most people will not think to click on the title on a post permalink page so I’d like a link in the post that says “Go to article…” without having to put it in there manually every time.
I’m trying to figure out how to, in the case of there being no url to go to, just place regular text instead of the regular permalink…
How would I go about doing this?
Thanks very much for this code. Supremely helpful for a nonprofit I’m volunteering with.
One question:
I’m trying to figure out how I could essentially duplicate this function to conditionally replace the text I have at the end of each post (which is currently a link to the post permalink). For example, when there is a custom field entry of a url, it would say “Visit site” and link to the custom url; when there isn’t it would say “Read more” and link to the permalink. Do I just need to add another variable at the top and echo the value?
Any help you can offer would be much appreciated.
Cheers,
Christopher
Thanks, this works great, but now my RSS feeds are broken as I would naturally want them to link to the same URL as the post rather than the post itself. How would I get similar functionality to occur in my RSS and ATOM feeds?
Should I just copy paste and modify this function and put it into wp-rss.php?
I’m in Adam’s situation. It works ok for the blog but not for the feed.
Trackbacks: