WordPress函数:get_post_permalink 获取文章的永久链接

江河/ 2023年06月24日/ WordPress/ 浏览 1571

函数原型:


get_post_permalink( int|WP_Post $post, bool $leavename = false, bool $sample = false ): string|false


获取文章的永久链接。


参数说明:


$post,文章或文章ID。


$leavename,是否保留文章名称。


$sample,是否是简化链接。


函数源码:


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

	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

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

	$slug = $post->post_name;

	$force_plain_link = wp_force_plain_post_permalink( $post );

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

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

	if ( ! empty( $post_link ) && ( ! $force_plain_link || $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 ) && ! $force_plain_link ) ) {
			$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 );
	}

	return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );
}


包含钩子:


apply_filters( 'post_type_link', string $post_link, WP_Post $post, bool $leavename, bool $sample )


发表评论

暂无评论,抢个沙发...

客服 工单