WordPress

WordPress get_permalink Example

While working on your WordPress theme or plugin, you might need to get the current posts permalink. Below, you will learn how to do that, and why not also getting another post’s permalink while we’re at it? Let’s find out how!

 
 
 
 
 

Definition and Syntax

The get_permalink() function makes it possible for you to retrieve the current post’s permalink. It’s syntax goes like below:

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

You see that this function takes two parameters, both optional. The first one, $id, is the one which allows you to manually modify the ID of the post, therefore making you able to choose the post whose permalink you want by it. Obviously, the default value of this parameter is the current post’s ID.

The second one, $leavename, is a boolean operator (that takes either TRUE or FALSE values) that defines the structure of the permalink you will get, or specifically, whether to include the post’s name or just keep the page’s name. To make it more clear, let’s suppose we have set this parameter to FALSE. Instead of getting a result similar to http://www.mywebsite.com/current-post, we will get this one: http://www.mywebsite.com/%currentpostname%. The default value for this parameter is FALSE.

Usage

We mentioned before that we can use the get_permalink() function to get the permalink of both the current post and also others’. Let’s see how we can do these in real life!

Showing current post’s permalink

The easiest and most straightforward way to use the function is to find out the current post’s permalink. See the code snippet below:

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

As you might notice, it’s just the syntax, we haven’t defined either parameter.

Showing a certain post’s permalink

We can choose the post whose permalink we want by specifying the $id parameter as seen in the code below:

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

This way, we have required the information regarding the post with the ID 16.

Showing a certain post’s permalink structure

We can find out a specific post’s permalink structure by displaying the post’s permalink with it’s name. Take a look at the code snippet below:

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

If the permalink of the post whose ID is 16 is something like http://www.mywebsite.com/sixteen when we use the code snippet above, then when we use this line of code it’ll be similar to http://www.mywebsite.com/2015/10/sixteen, or whatever permalink structure you already have set.

Download the source code

This was an example of get_permalink in WordPress.

Download the source code for this tutorial:

Download
You can download the full source code of this example here : GetPermalink

Era Balliu

Era is a Telecommunications Engineering student, with a great passion for new technologies. Up until now she has been coding with HTML/CSS, Bootstrap and other front-end coding languages and frameworks, and her recent love is Angular JS.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button