WordPress the_permalink() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Prints a URL

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.

WordPress
<a href="<?php the_permalink(); ?>">Read more</a>
CallWhat 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
WordPress
the_permalink( int|WP_Post $post = 0 ): void

Parameters

ParameterDescription
$postint|WP_Post. A post ID or object. Default is 0, which means the current post.

Return Value

Return ValueDescription
voidNothing 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.

WordPress
<?php
while ( have_posts() ) {
  the_post();
  ?>
  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <?php
}

How It Works

  1. The Loop sets the current post.
  2. the_permalink() prints its URL inside href.
  3. the_title() prints the link text.

Example 2 — Print the URL of another post

Pass a post ID to link to any post.

WordPress
<a href="<?php the_permalink( 15 ); ?>">About us</a>

How It Works

  1. 15 is the ID of the post or page.
  2. The function prints that post's URL.
  3. This works outside the Loop too.

Example 3 — Make a Read more button

Use it in a list of posts.

WordPress
<a class="btn" href="<?php the_permalink(); ?>">Read more</a>

How It Works

  1. The anchor has your own CSS class.
  2. the_permalink() fills the URL for each post in the Loop.
  3. 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.

Next: the_privacy_policy_link()

Print the Privacy Policy link, if a policy page is set.

the_privacy_policy_link() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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