返回介绍

wp_get_shortlink()

发布于 2017-09-11 12:08:50 字数 5186 浏览 1080 评论 0 收藏 0

wp_get_shortlink( int $id,  string $context = 'post',  bool $allow_slugs = true )

Returns a shortlink for a post, page, attachment, or site.


description

This function exists to provide a shortlink tag that all themes and plugins can target. A plugin must hook in to provide the actual shortlinks. Default shortlink support is limited to providing ?p= style links for posts. Plugins can short-circuit this function via the ‘pre_get_shortlink’ filter or filter the output via the ‘get_shortlink’ filter.


参数

$id

(int) (Optional) A post or site id. Default is 0, which means the current post or site.

$context

(string) (Optional) Whether the id is a 'site' id, 'post' id, or 'media' id. If 'post', the post_type of the post is consulted. If 'query', the current query is consulted to determine the id and context.

Default value: 'post'

$allow_slugs

(bool) (Optional) Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this.

Default value: true


返回值

(string) A shortlink or an empty string if no shortlink exists for the requested re源代码 or if shortlinks are not enabled.


源代码

File: wp-includes/link-template.php

function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) {
	/**
	 * Filters whether to preempt generating a shortlink for the given post.
	 *
	 * Passing a truthy value to the filter will effectively short-circuit the
	 * shortlink-generation process, returning that value instead.
	 *
	 * @since 3.0.0
	 *
	 * @param bool|string $return      Short-circuit return value. Either false or a URL string.
	 * @param int         $id          Post ID, or 0 for the current post.
	 * @param string      $context     The context for the link. One of 'post' or 'query',
	 * @param bool        $allow_slugs Whether to allow post slugs in the shortlink.
	 */
	$shortlink = apply_filters( 'pre_get_shortlink', false, $id, $context, $allow_slugs );

	if ( false !== $shortlink ) {
		return $shortlink;
	}

	$post_id = 0;
	if ( 'query' == $context && is_singular() ) {
		$post_id = get_queried_object_id();
		$post = get_post( $post_id );
	} elseif ( 'post' == $context ) {
		$post = get_post( $id );
		if ( ! empty( $post->ID ) )
			$post_id = $post->ID;
	}

	$shortlink = '';

	// Return p= link for all public post types.
	if ( ! empty( $post_id ) ) {
		$post_type = get_post_type_object( $post->post_type );

		if ( 'page' === $post->post_type && $post->ID == get_option( 'page_on_front' ) && 'page' == get_option( 'show_on_front' ) ) {
			$shortlink = home_url( '/' );
		} elseif ( $post_type->public ) {
			$shortlink = home_url( '?p=' . $post_id );
		}
	}

	/**
	 * Filters the shortlink for a post.
	 *
	 * @since 3.0.0
	 *
	 * @param string $shortlink   Shortlink URL.
	 * @param int    $id          Post ID, or 0 for the current post.
	 * @param string $context     The context for the link. One of 'post' or 'query',
	 * @param bool   $allow_slugs Whether to allow post slugs in the shortlink. Not used by default.
	 */
	return apply_filters( 'get_shortlink', $shortlink, $id, $context, $allow_slugs );
}

Collapse full 源代码 code View on Trac


相关函数

Uses

  • wp-includes/query.php: is_singular()
  • wp-includes/query.php: get_queried_object_id()
  • wp-includes/link-template.php: pre_get_shortlink
  • wp-includes/link-template.php: get_shortlink
  • wp-includes/link-template.php: home_url()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/option.php: get_option()
  • wp-includes/post.php: get_post()
  • wp-includes/post.php: get_post_type_object()
  • Show 4 more uses Hide more uses

Used By

  • wp-includes/link-template.php: wp_shortlink_wp_head()
  • wp-includes/link-template.php: wp_shortlink_header()
  • wp-includes/link-template.php: the_shortlink()
  • wp-includes/admin-bar.php: wp_admin_bar_shortlink_menu()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example
    Display the short link in plain text such as “Short URL: http://example.com/?p=1234”

    
    Short URL: <?php echo wp_get_shortlink(); ?> 
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文