WordPress the_permalink() Function

Definition
In WordPress, the_permalink() prints the URL of a post. It is one of the most used template functions. You put it inside the href of a link.
The URL is already escaped, so you can print it right away. Its twin, get_permalink(), returns the URL instead. Remember: the_ prints, get_ returns.
Syntax
Use it inside the href in the Loop.
<a href="<?php the_permalink(); ?>">Read more</a>
| Call | What it does |
|---|
the_permalink() | Print the URL of the current post. |
the_permalink( 15 ) | Print the URL of post 15. |
<a href="<?php the_permalink(); ?>"> | The common pattern for a post link. |
It prints the URL. It does not return anything, so never write echo the_permalink().
Full declaration
the_permalink( int|WP_Post $post = 0 ): void
Parameters
| Parameter | Description |
|---|
$post | int|WP_Post. A post ID or object. Default is 0, which means the current post. |
Return Value
| Return Value | Description |
|---|
| void | Nothing is returned. The URL is printed. |
WordPress Examples
Three short examples. Each one shows the code and what it prints or does.
Example 1 — Link the post title
The classic Loop example.
<?php
while ( have_posts() ) {
the_post();
?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php
}
<h2><a href="https://example.com/hello-world/">Hello world</a></h2>
How It Works
- The Loop sets the current post.
the_permalink() prints its URL inside href.the_title() prints the link text.
Example 2 — Print the URL of another post
Pass a post ID to link to any post.
<a href="<?php the_permalink( 15 ); ?>">About us</a>
<a href="https://example.com/about/">About us</a>
How It Works
15 is the ID of the post or page.- The function prints that post's URL.
- This works outside the Loop too.
Example 3 — Make a Read more button
Use it in a list of posts.
<a class="btn" href="<?php the_permalink(); ?>">Read more</a>
How It Works
- The anchor has your own CSS class.
the_permalink() fills the URL for each post in the Loop.- Every post gets its own button.
Frequently Asked Questions
It prints the URL of a post. You usually use it inside the href of a link.
the_permalink() prints the URL. get_permalink() returns it, so you can store it or change it.
No. The URL is already escaped. You need esc_url() for get_permalink().
Yes. Pass a post ID, such as the_permalink( 15 ).
the_permalink() already prints, and returns nothing. Echoing it does no harm, but it is not needed.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful