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

Press Ctrl+V to copy the following code.

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Do you want to add an external link as post title in WordPress? Sometimes you may just want to share a link with your users. Instead of sending them to a post, you may want the post title to link to the other website. In this article, we will show you 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.

Click here to show/hide the code.
 
  1. function print_post_title() {
  2. global $post;
  3. $thePostID = $post->ID;
  4. $post_id = get_post($thePostID);
  5. $title = $post_id->post_title;
  6. $perm = get_permalink($post_id);
  7. $post_keys = array(); $post_val = array();
  8. $post_keys = get_post_custom_keys($thePostID);
  9. if (!empty($post_keys)) {
  10. foreach ($post_keys as $pkey) {
  11. if ($pkey=='external_url') {
  12. $post_val = get_post_custom_values($pkey);
  13. }
  14. }
  15. if (empty($post_val)) {
  16. $link = $perm;
  17. } else {
  18. $link = $post_val[0];
  19. }
  20. } else {
  21. $link = $perm;
  22. }
  23. echo '<h2><a href="'.$link.'" rel="bookmark" title="'.$title.'">'.$title.'</a></h2>';
  24. }

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

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:

Click here to show/hide the code.
 
  1. <?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>

You need to replace it with this code:

Click here to show/hide the code.
 
  1. <?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 fieldsmeta box.

If you cannot see the custom fields meta box, then you need to click Screen Optionsin 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

Click here to show/hide the code.
 
  1. 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

Click here to show/hide the code.
 
  1. external_url
custom field from the drop down menu and enter your external link in the value field.

Was this article helpful?

Related Articles

Leave A Comment?