Using WordPress ‘get_the_permalink()’ PHP function

The get_the_permalink() WordPress PHP function retrieves the full permalink for the current post or post ID.

Usage

get_the_permalink( $post, $leavename );

Parameters

  • $post (int|WP_Post, optional) – Post ID or post object. Default is the global $post.
  • $leavename (bool, optional) – Whether to keep post name or page name. Default: false.

More information

See WordPress Developer Resources: get_the_permalink()

Examples

Get the permalink of the current post

This code retrieves the permalink of the current post in the loop.

// Get the permalink
$permalink = get_the_permalink();
// Display the permalink
echo $permalink;

Get the permalink of a specific post by ID

This code retrieves the permalink of a post with the ID 42.

// Get the permalink of post with ID 42
$permalink = get_the_permalink(42);
// Display the permalink
echo $permalink;

Get the permalink using a WP_Post object

This code retrieves the permalink of a post using a WP_Post object.

// Get the WP_Post object for post with ID 42
$post_object = get_post(42);
// Get the permalink using the WP_Post object
$permalink = get_the_permalink($post_object);
// Display the permalink
echo $permalink;

Get the permalink without altering the post or page name

This code retrieves the permalink of the current post without changing the post or page name.

// Get the permalink without altering post or page name
$permalink = get_the_permalink(null, true);
// Display the permalink
echo $permalink;

This code retrieves the permalink of the current post and displays it as a link.

// Get the permalink
$permalink = get_the_permalink();
// Display the permalink as a link
echo '<a href="' . $permalink . '">Read More</a>';