返回介绍

get_post_permalink()

发布于 2017-09-10 23:55:24 字数 3826 浏览 1118 评论 0 收藏 0

get_post_permalink( int $id,  bool $leavename = false,  bool $sample = false )

Retrieves the permalink for a post of a custom post type.


description


参数

$id

(int) (Optional) Post ID. Default uses the global $post.

$leavename

(bool) (Optional) defaults to false. Whether to keep post name.

Default value: false

$sample

(bool) (Optional) defaults to false. Is it a sample permalink.

Default value: false


返回值

(string|WP_Error) The post permalink.


源代码

File: wp-includes/link-template.php

function get_post_permalink( $id = 0, $leavename = false, $sample = false ) {
	global $wp_rewrite;

	$post = get_post($id);

	if ( is_wp_error( $post ) )
		return $post;

	$post_link = $wp_rewrite->get_extra_permastruct($post->post_type);

	$slug = $post->post_name;

	$draft_or_pending = get_post_status( $id ) && in_array( get_post_status( $id ), array( 'draft', 'pending', 'auto-draft', 'future' ) );

	$post_type = get_post_type_object($post->post_type);

	if ( $post_type->hierarchical ) {
		$slug = get_page_uri( $id );
	}

	if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) {
		if ( ! $leavename ) {
			$post_link = str_replace("%$post->post_type%", $slug, $post_link);
		}
		$post_link = home_url( user_trailingslashit($post_link) );
	} else {
		if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) )
			$post_link = add_query_arg($post_type->query_var, $slug, '');
		else
			$post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), '');
		$post_link = home_url($post_link);
	}

	/**
	 * Filters the permalink for a post of a custom post type.
	 *
	 * @since 3.0.0
	 *
	 * @param string  $post_link The post's permalink.
	 * @param WP_Post $post      The post in question.
	 * @param bool    $leavename Whether to keep the post name.
	 * @param bool    $sample    Is it a sample permalink.
	 */
	return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
}

更新日志

Versiondescription
3.0.0Introduced.

More Information

Basic Usage


<?php get_post_permalink( $id, $leavename, $sample ); ?>

See also get_permalink().


相关函数

Uses

  • wp-includes/functions.php: add_query_arg()
  • wp-includes/link-template.php: home_url()
  • wp-includes/link-template.php: post_type_link
  • wp-includes/link-template.php: user_trailingslashit()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: get_page_uri()
  • wp-includes/post.php: get_post()
  • wp-includes/post.php: get_post_status()
  • wp-includes/post.php: get_post_type_object()
  • wp-includes/class-wp-rewrite.php: WP_Rewrite::get_extra_permastruct()
  • wp-includes/load.php: is_wp_error()
  • Show 6 more uses Hide more uses

Used By

  • wp-admin/includes/class-wp-press-this.php: WP_Press_This::save_post()
  • wp-includes/link-template.php: get_permalink()

User Contributed Notes

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

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

发布评论

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