WordPress function get_the_shortlink

Many WordPress functions have a standard version which outputs the results directly and an additional version prefixed with get_ that returns the output – useful for adding to a string and manipulating before output.

One such exclusion is the_shortlink, which returns a short link to any page or post – there is no get_the_shortlink, so many people have to resort to just outputting the result in a FORM field for people to cut & paste.  However, here is a quick way to add the capability to your blog.

Simply add the following 5 lines to your theme’s function.php file…

function get_the_shortlink() {
$post_id = get_the_ID();
if ($post_id!="") {$shortlink=home_url()."/?p=".$post_id;} else {$shortlink="";}
return $shortlink;
view raw functions.php hosted with ❤ by GitHub

Now you just need to call get_the_shortlink() from within your WordPress loop and it will return the shortlink URL.

Unlike the_shortlink it doesn’t accept any parameters, but this is a quick and simple solution.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: