Using WordPress ‘get_page_link()’ PHP function

The get_page_link() WordPress PHP function retrieves the permalink for the current page or page ID.

Usage

get_page_link( $post, $leavename, $sample );

Parameters

  • $post (int|WP_Post) – Optional. Post ID or object. Default uses the global $post. Default: false.
  • $leavename (bool) – Optional. Whether to keep the page name. Default: false.
  • $sample (bool) – Optional. Whether it should be treated as a sample permalink. Default: false.

More information

See WordPress Developer Resources: get_page_link()

Examples

In this example, we get the permalink for the page with ID 40.

$permalink = get_page_link( 40 );
echo $permalink; // Output: https://example.com/page-name/

Create a link to the page with ID 40 using get_page_link() function.

<a href="<?php echo esc_url( get_page_link( 40 ) ); ?>"><?php esc_html_e( 'Map', 'textdomain' ); ?></a>

Get the permalink for the global $post

In this example, we get the permalink for the global $post object.

$permalink = get_page_link();
echo $permalink; // Output: https://example.com/current-page/

Get the permalink for the blog page.

$page_for_posts = get_option( 'page_for_posts' );
?>
<a href="<?php echo esc_attr( esc_url( get_page_link( $page_for_posts ) ) ) ?>"><?php esc_html_e( 'Blog Page', 'textdomain' ) ?></a>

In this example, we get the permalink for the page with ID 50 without the page name.

$permalink = get_page_link( 50, true );
echo $permalink; // Output: https://example.com/?page_id=50