WordPress at a glance

get_permalink() WP 1.0.0

Gets a permanent link to the current or specified post, for further use in php.

It does not display a link to the screen but returns it to a variable.

Can be used outside of the WordPress Loop, for this, you need to specify the first parameter $id - ID of the post, the link of which you want to get.

Gets the URL of any post type, including static pages and newly created post types.

If you do not specify an ID parameter and use this function outside of the WordPress Loop on archive pages (categories, archive by date, archive by authors, etc.), the function will return a link to the last post displayed in the current page Loop, but not a link to the current page.

✈ 1 time = 0.002181s = very slow | 50000 times = 4.98s = fast PHP 7.1.11, WP 4.9.8
Hooks from function:
Return

String/false. The permalink URL or false if post does not exist.

Usage

get_permalink( $post, $leavename );
$post(int/WP_Post)
Post ID or post object, which link we want to retrieve.
Default: is the global $post
$leavename(true/false)
Do not replace the name of the post in the link, i.e. do not replace the tag %postname% with the name of the post.
Default: false

Examples

#1 Get a link to the current post

Get a link to the post (inside the WordPress Loop). Since the function does not output data, you need to use php echo command:

<?php echo get_permalink(); ?>

It is equivalent to:

<?php the_permalink(); ?>

#2 Get links to certain posts

Let's output a list where we get links to pages with ID 1 and 10.

<ul>
	<li><a href="<?php echo get_permalink(1); ?>">О блоге</a></li>
	<li><a href="<?php echo get_permalink(10); ?>">Об авторе</a></li>
</ul>

#3 Use with the second parameter

Leave the tag %postname% unchanged, for example, we have such permalink structure /%post_id%/%postname%, then the function will work like this:

<?php echo get_permalink( 185, true ); ?>

// displays: http://site.ru/185/%postname%

Code of get_permalink: wp-includes/link-template.php VER 4.9.8

<?php
function get_permalink( $post = 0, $leavename = false ) {
	$rewritecode = array(
		'%year%',
		'%monthnum%',
		'%day%',
		'%hour%',
		'%minute%',
		'%second%',
		$leavename? '' : '%postname%',
		'%post_id%',
		'%category%',
		'%author%',
		$leavename? '' : '%pagename%',
	);

	if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) {
		$sample = true;
	} else {
		$post = get_post( $post );
		$sample = false;
	}

	if ( empty($post->ID) )
		return false;

	if ( $post->post_type == 'page' )
		return get_page_link($post, $leavename, $sample);
	elseif ( $post->post_type == 'attachment' )
		return get_attachment_link( $post, $leavename );
	elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )
		return get_post_permalink($post, $leavename, $sample);

	$permalink = get_option('permalink_structure');

	/**
	 * Filters the permalink structure for a post before token replacement occurs.
	 *
	 * Only applies to posts with post_type of 'post'.
	 *
	 * @since 3.0.0
	 *
	 * @param string  $permalink The site's permalink structure.
	 * @param WP_Post $post      The post in question.
	 * @param bool    $leavename Whether to keep the post name.
	 */
	$permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );

	if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ) ) ) {
		$unixtime = strtotime($post->post_date);

		$category = '';
		if ( strpos($permalink, '%category%') !== false ) {
			$cats = get_the_category($post->ID);
			if ( $cats ) {
				$cats = wp_list_sort( $cats, array(
					'term_id' => 'ASC',
				) );

				/**
				 * Filters the category that gets used in the %category% permalink token.
				 *
				 * @since 3.5.0
				 *
				 * @param WP_Term  $cat  The category to use in the permalink.
				 * @param array    $cats Array of all categories (WP_Term objects) associated with the post.
				 * @param WP_Post  $post The post in question.
				 */
				$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );

				$category_object = get_term( $category_object, 'category' );
				$category = $category_object->slug;
				if ( $parent = $category_object->parent )
					$category = get_category_parents($parent, false, '/', true) . $category;
			}
			// show default category in permalinks, without
			// having to assign it explicitly
			if ( empty($category) ) {
				$default_category = get_term( get_option( 'default_category' ), 'category' );
				if ( $default_category && ! is_wp_error( $default_category ) ) {
					$category = $default_category->slug;
				}
			}
		}

		$author = '';
		if ( strpos($permalink, '%author%') !== false ) {
			$authordata = get_userdata($post->post_author);
			$author = $authordata->user_nicename;
		}

		$date = explode(" ",date('Y m d H i s', $unixtime));
		$rewritereplace =
		array(
			$date[0],
			$date[1],
			$date[2],
			$date[3],
			$date[4],
			$date[5],
			$post->post_name,
			$post->ID,
			$category,
			$author,
			$post->post_name,
		);
		$permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) );
		$permalink = user_trailingslashit($permalink, 'single');
	} else { // if they're not using the fancy permalink option
		$permalink = home_url('?p=' . $post->ID);
	}

	/**
	 * Filters the permalink for a post.
	 *
	 * Only applies to posts with post_type of 'post'.
	 *
	 * @since 1.5.0
	 *
	 * @param string  $permalink The post's permalink.
	 * @param WP_Post $post      The post in question.
	 * @param bool    $leavename Whether to keep the post name.
	 */
	return apply_filters( 'post_link', $permalink, $post, $leavename );
}

Related Functions

From tag: permalinks (clean friendly URL)

More from category: Posts, pages...

More from Template Tags: Links (URL)

No comments
    Hello, !     Log In . Register