Get WordPress Short Link And Full Link To A Post
Posted by in Tips And Tricks March 31, 2011 1 Comment

There are 2 ways to get the Short Link or Full Link (permalink) to a post by using wp_get_shortlink() or get_permalink() function respectively.

Some case you want to get the link of the current page/post or a particular page/post with specified ID for sharing the link to Social Media site such as Facebook, Twitter, Digg and other bookmarking services you can use either of following ways:

1. Get the Full Link – permalink

get_permalink() function has a input parameter named ID as optional gets the full link of a page/post. It will return the link of the current page/post if we use the function without the ID.

Example:

1. Link to current page/post:

<?php
	echo "Full link for this post: <br>";
	echo get_permalink();
	// return: http://4rapiddev.com/tips-and-tricks/get-wordpress-short-link-and-full-link-to-a-post/
?>

2. Link to page ID 9 and post ID 512:

<?php
	echo "Full link for post ID 512: <br>";
	echo get_permalink(512) . "<br>";
	// return: http://4rapiddev.com/internet/lg-optimus-2x-key-specifications/
 
	echo "Full link for page ID 9: <br>";
	echo get_permalink(9) . "<br>";
	// return: http://4rapiddev.com/about/
?>

2. Get Short Link

wp_get_shortlink() function is specially useful when you want to share the link on Twitter or other service which limit characters.

<?php
	echo wp_get_shortlink();
	// return: http://4rapiddev.com/?p=525
?>

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at hoan@4rapiddev.com
  • http://www.bing.com/ Ricky

    I found mseylf nodding my noggin all the way through.