Created
November 15, 2017 18:32
-
-
Save dartiss/e7795c4cd2594c4d9b17027349f73833 to your computer and use it in GitHub Desktop.
WordPress function get_the_shortlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_the_shortlink() { | |
$post_id = get_the_ID(); | |
if ($post_id!="") {$shortlink=home_url()."/?p=".$post_id;} else {$shortlink="";} | |
return $shortlink; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 noget_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'sfunctions.php
. Now you just need to callget_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.