close
externallinkposttitle

At times the owner of a site at may just want to post share the link with 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 link to external links from the post title in WordPress.

Method 1: Linking Post Title to an External Link in WordPress using Plugin

This is the easier method which can be used by anyone. First thing you need to do is install and activate the Page Links To plugin. When activated click on new post or edit post and you will notice the ‘Page Links To’ meta box below the post editor.

pagelinksto

Click on ‘A custom URL’ to add the link you want to add to post title. Now you can save or publish your post. 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 is a more advance and complex method.  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.

01function print_post_title() {

 

02global $post;

 

03$thePostID = $post->ID;

 

04$post_id = get_post($thePostID);

 

05$title = $post_id->post_title;

 

06$perm = get_permalink($post_id);

 

07$post_keys = array(); $post_val = array();

 

08$post_keys = get_post_custom_keys($thePostID);

 

09 

 

10if (!empty($post_keys)) {

 

11foreach ($post_keys as $pkey) {

 

12if ($pkey==’external_url’) {

 

13$post_val = get_post_custom_values($pkey);

 

14}

 

15}

 

16if (empty($post_val)) {

 

17$link = $perm;

 

18} else {

 

19$link = $post_val[0];

 

20}

 

21} else {

 

22$link = $perm;

 

23}

 

24echo ‘<h2><a href=”‘.$link.'” rel=”bookmark” title=”‘.$title.'”>’.$title.'</a></h2>’;

 

25}

 

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.

1<?php the_title( sprintf( ‘<h2 class=”entry-title”><a href=”%s” rel=”bookmark”>’, esc_url( get_permalink() ) ), ‘</a></h2>’ ); ?>

 

Replace it with this.

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 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 where you need to check the box next to ‘Custom Fields’.

screenoptions

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.

addnewcf

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.

 

 

Tags : wordpressWordPress how to
Nooruddin Ahmed

The author Nooruddin Ahmed

An avid football fan. Lives for the weekend game.Aviation enthusiast. Believes in letting bygones be bygones.

Leave a Response