wp_get_shortlink
函数
wp_get_shortlink ( $id = 0, $context = 'post', $allow_slugs = true )
- 参数
-
-
(int)
$id
Optional. A post or site ID. Default is 0, which means the current post or site.- Required: 否
-
(string)
$context
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 ‘post’.- Required: 否
- Default: ‘post’
-
(bool)
$allow_slugs
Optional. Whether to allow post slugs in the shortlink. It is up to the plugin how and whether to honor this. Default true.- Required: 否
- Default: true
-
(int)
- 返回值
-
- (string) A shortlink or an empty string if no shortlink exists for the requested resource or if shortlinks are not enabled.
- 定义位置
-
-
wp-includes/link-template.php
, line 4031
-
wp-includes/link-template.php
- 引入
- 3.0.0
- 弃用
- –
返回一个文章、页面、附件或网站的短链接。
这个函数的存在是为了提供一个所有主题和插件都可以针对的短链接标签。一个插件必须挂钩以提供实际的短链接。默认的短链接支持只限于为文章提供?p=风格的链接。插件可以通过{@see ‘pre_get_shortlink’}过滤器短路这个函数,或者通过{@see ‘get_shortlink’}过滤器过滤输出。
function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { /** * Filters whether to preempt generating a shortlink for the given post. * * Returning a value other than false from the filter will short-circuit * the shortlink generation process, returning that value instead. * * @since 3.0.0 * * @param false|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 && get_option( 'page_on_front' ) == $post->ID && 'page' === get_option( 'show_on_front' ) ) { $shortlink = home_url( '/' ); } elseif ( $post_type && $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 ); }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。