(+84) 463.28.7979

How to Link to External Links from the Post Title in WordPress

Blogging is all about sharing resources, and often it is the case where blogger wants to just link to an external resource because he/she finds it useful to their readers. The main problem they face is that they have to make a new post in order to just tell the readers to go to another site. We have a solution for this problem and that is through a custom field hack which allows the WordPress blogger to link to the external resources in their post title. So when their user clicks on the post title, it takes them to another article.

This tutorial can help creating a resource section on your blog if you want to link to external resources, and it is a doorway to a lot more customization in WordPress if thought out correctly.

First thing you need to do is open your functions.php which is found in your template folder where other files like single.php and page.php is located. 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>’;

Now you would need to open your index.php and find the following code or something similar:

<h2><a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a></h2>

Change it to:

<?php print_post_title() ?>

Once you have done that, upload both files to your webhost.

Now when you are writing a post, scroll down to where it says Custom Fields. Find the name: url1, title_url, or url_title and add the url to the external resource. Add a short description if you so desire, and hit publish.

Don’t be afraid, this function does not take away your normal post title links, all it does is add an extra query which checks for custom field for external links. If the external link is not included, it refers to the default code and link to the normal post page.

Update: We were informed by our user that there is a plugin for those users who don’t want to do the manual theme hacking by following this tutorial.

It is called Page Links To

Check it out and use that if you don’t want to deal with the codes.

This tutorial was requested by Tamar Weinberg who writes at Techipedia, and also work with Mashable Magazine. We encourage our users to ask us questions because their questions not only help them, but it helps the WordPress community as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>