Using WordPress ‘get_page_uri()’ PHP function

The get_page_uri() WordPress PHP function builds the URI path for a page, including its parent pages if any.

Usage

get_page_uri( $page );

Input:

  • $page (WP_Post|object|int): Page ID or WP_Post object. Default is global $post.

Output:

  • The URI path of the specified page.

Parameters

  • $page (WP_Post|object|int, Optional): Page ID or WP_Post object. Default is global $post.

More information

See WordPress Developer Resources: get_page_uri()

Examples

Get URI of a specific page by ID

Retrieve the URI of a page with the ID 42 and display it as a link.

$page_id = 42;
$uri = get_page_uri($page_id);
echo '<a href="' . esc_url($uri) . '">' . esc_html__('Page 42', 'textdomain') . '</a>';

Get URI of the current page

Retrieve the URI of the current page and display it as a link.

$uri = get_page_uri();
echo '<a href="' . esc_url($uri) . '">' . esc_html__('Current Page', 'textdomain') . '</a>';

Get URI of a WP_Post object

Retrieve the URI of a specific WP_Post object and display it as a link.

$post_obj = get_post(42);
$uri = get_page_uri($post_obj);
echo '<a href="' . esc_url($uri) . '">' . esc_html__('Page from WP_Post', 'textdomain') . '</a>';

Get URI of a page within a loop

Within a loop, retrieve the URI of each page and display it as a link.

while (have_posts()) : the_post();
    $uri = get_page_uri();
    echo '<a href="' . esc_url($uri) . '">' . esc_html__(get_the_title(), 'textdomain') . '</a>';
endwhile;

Get URI of a page’s parent

Retrieve the URI of the parent of a specific page and display it as a link.

$page_id = 42;
$parent_id = wp_get_post_parent_id($page_id);
$uri = get_page_uri($parent_id);
echo '<a href="' . esc_url($uri) . '">' . esc_html__('Parent Page', 'textdomain') . '</a>';