the_shortlink()WP 3.0.0

Outputs a short link to the current post, for example: /?p=1234.

This template tag outputs a text link that leads to the current post, but in the URL of the link, a short link format is used: /?p=1234.

Such links are logically used for sites where pretty permalinks (ЧПУ) are used.

If you need to get exactly the short URL, use the function: wp_get_shortlink( $post->ID ).

Even if you have pretty permalinks enabled on your site, such short links from other sites to yours are considered by search engines to be the same as regular links because short links use a 301 redirect to the actual link.

1 time — 0.001029 sec (very slow) | 50000 times — 4.65 sec (fast) | PHP 7.1.2, WP 4.7.3
Hooks from the function

Returns

null. html tag A - a link to the current post.

Usage

<?php the_shortlink( $text, $title, $before, $after ); ?>
$text(string)
Text of the link.
Default: 'Это короткая ссылка.' ('This is the short link.')
$title(string)
Text for the title attribute of the tag
Default: Заголовок записи ('Post title')
$before(string)
Text before the link.
Default: ''
$after(string)
Text after the link.
Default: ''

Examples

1

#1 Display a short link with the text "This is the short link":

<?php the_shortlink(); ?>

We get it:

<a rel="shortlink" href="http://example.com/?p=1234" title="post title">This is the shortlink</a>
0

#2 Display the link inside the html tags

Tags will only appear when a short link is displayed:

<?php the_shortlink( 'shortlink', null, '<div class="shortlink">', '</div>'); ?

We get it:

<div class='shortlink'><a rel="shortlink" href="http://example.com/?p=1234" title="title posts">shortlink</a></div>

Changelog

Since 3.0.0 Introduced.
Since 6.8.0 Removed title attribute.

the_shortlink() code WP 7.0

function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
	$post = get_post();

	if ( empty( $text ) ) {
		$text = __( 'This is the short link.' );
	}

	$shortlink = wp_get_shortlink( $post->ID );

	if ( ! empty( $shortlink ) ) {
		$link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '">' . $text . '</a>';

		/**
		 * Filters the short link anchor tag for a post.
		 *
		 * @since 3.0.0
		 *
		 * @param string $link      Shortlink anchor tag.
		 * @param string $shortlink Shortlink URL.
		 * @param string $text      Shortlink's text.
		 * @param string $title     Shortlink's title attribute. Unused.
		 */
		$link = apply_filters( 'the_shortlink', $link, $shortlink, $text, $title );
		echo $before, $link, $after;
	}
}