In this tutorial, we will learn how to link to external links from the post title in WordPress.
Method 1: Linking Post Title to an External Link in WordPress using Plugin
This method is easier and is recommended for beginners.
First thing you need to do is install and activate the Page Links To plugin. For more details, see our step by step guide on how to install a WordPress plugin.
Upon activation, simply create a new post or edit an existing one. You will notice the new ‘Page Links To’ meta box below the post editor.
Click on ‘A custom URL’ to add the link you want to add to post title. Now you can save or publish your post.
That’s all. The post title will now link to the custom URL you provided.
It is not necessary to use it for external links only. You can also use it to send users to different posts and pages on your WordPress site.
Method 2: Add External Link to Post Title Using Code
This method requires you to add code to your WordPress site. You can use this method if you are comfortable with pasting snippets from web into WordPress.
Simply add this code to your theme’s functions.php file or a site-specific plugin.
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=='external_url') { $post_val = get_post_custom_values($pkey); } } if (empty($post_val)) { $link = $perm; } else { $link = $post_val[0]; } } else { $link = $perm; } echo ' '.$title.' '; }
This code looks simply looks for a custom field containing your custom URL. If the post has the custom field, then it outputs the post title linked to your URL.
The next step is to replace your theme’s default display of post title with this function. You will find it in archives.php, content.php, category.php and other templates. It would look something like this:
?php the_title( sprintf( '</p> <h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2> <p>' ); ?
You need to replace it with this code:
?php print_post_title() ?
The code part is over, now you need to add the external URL to the post. Simply edit the post or create a new one. On the post editor page, look for the custom fields meta box.
If you cannot see the custom fields meta box, then you need to click Screen Options in the top right corner of the screen. This will bring down a menu where you need to check the box next to ‘Custom Fields’.
You will find the custom fields meta box below the post editor.
Click on ‘Enter New’ and then enter external_url
in the ‘Name’ field and the URL you want to add to post title in the ‘Value’ field.
You can now save or publish your post. That’s all, your post title will now be linked to the URL you added in the custom field.
Next time you need to add a link, you just need to select the external_url
custom field from the drop down menu and enter your external link in the value field.
We hope this tutorial helped you learn how to link to external links from the post title in WordPress.