Skip to content

Instantly share code, notes, and snippets.

@dartiss
Created November 15, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dartiss/e7795c4cd2594c4d9b17027349f73833 to your computer and use it in GitHub Desktop.
Save dartiss/e7795c4cd2594c4d9b17027349f73833 to your computer and use it in GitHub Desktop.
WordPress function get_the_shortlink
<?php
function get_the_shortlink() {
$post_id = get_the_ID();
if ($post_id!="") {$shortlink=home_url()."/?p=".$post_id;} else {$shortlink="";}
return $shortlink;
}
?>
@dartiss
Copy link
Author

dartiss commented Nov 15, 2017

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 this code to your theme's functions.php. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment