How to link to an external link from the WordPress post title

How to link to an external link from the WordPress post title

Did you see other sites link to external posts from their post title? Well that is because it is completely useless to create a new post where inside you are going to tell users to go to another site to read it. You are wasting your user’s time. This trick will allow you to link to external links from your post title in WordPress.

Add the following to you functions.php file:

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>';
}

Then add the following tag in your template where you want to display the link

<?php print_post_title(); ?>

WordPress Hosting

Comments

Leave a Reply

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

WordPress Snippet Info

Tested with Wordpress v. 4.8
Review Status: Needs Review

How to link to an external link from the WordPress post title
4.8 (95%) 16 vote[s]
Please report broken snippets.
Report Broken Snippet