get_page_link() WordPress Function

The get_page_link function is used to retrieve the permalink for a page. This function takes a page id as its only parameter and returns a permalink for that page. This function is useful for creating links to pages on your website.

get_page_link( int|WP_Post $post = false, bool $leavename = false, bool $sample = false ) #

Retrieves the permalink for the current page or page ID.


Description

Respects page_on_front. Use this one.


Top ↑

Parameters

$post

(int|WP_Post)(Optional) Post ID or object. Default uses the global $post.

Default value: false

$leavename

(bool)(Optional) Whether to keep the page name.

Default value: false

$sample

(bool)(Optional) Whether it should be treated as a sample permalink.

Default value: false


Top ↑

Return

(string) The page permalink.


Top ↑

Source

File: wp-includes/link-template.php

function get_page_link( $post = false, $leavename = false, $sample = false ) {
	$post = get_post( $post );

	if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $post->ID ) {
		$link = home_url( '/' );
	} else {
		$link = _get_page_link( $post, $leavename, $sample );
	}

	/**
	 * Filters the permalink for a page.
	 *
	 * @since 1.5.0
	 *
	 * @param string $link    The page's permalink.
	 * @param int    $post_id The ID of the page.
	 * @param bool   $sample  Is it a sample permalink.
	 */
	return apply_filters( 'page_link', $link, $post->ID, $sample );
}


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More