Within your WordPress theme or plugin, you may need to obtain the post permalink. In this article, we will introduce you to the get_permalink() function which you may use to obtain the permalink of either your current post or another desired post.

If you would like more information on creating WordPress plugins, see our tutorial series on creating your first WordPress plugin.

Basic usage

<?php $permalink = get_permalink( $id ); ?>

This this example, we are simply getting the post permalink from a desired post using the $id parameter, and assigning it to the $permalink variable.

Parameters

$id: This optional parameter allows you to manually define the ID of the post that you want to get the permalink of. This parameter takes an integer such as 5 for post ID 5.

$leavename: This optional parameter takes a true or false boolean operator. Instead of displaying the full permalink, it will display your permalink structure such as http://example.com/%postname%/.

Examples

Display the permalink of a specific post

In the following code example, we are getting the permalink of the post with ID 5, and assigning it to a variable. Then, we are displaying the contents of that variable:

<?php
$permalink = get_permalink(5);
echo $permalink;
?>

Display the permalink structure of a specific post

In this example, we are getting the permalink structure of the post with ID 5 and assigning it to a new variable. Then, we are displaying that permalink structure:

<?php
$permalink = get_permalink(5, true);
echo $permalink;
?>

References

For more information on the get_permalink() WordPress function, take a look at the get_permalink() WordPress Codex page.
Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!