WordPress函数:get_permalink 获取文章的链接

江河/ 2023年06月23日/ WordPress/ 浏览 2323

函数原型:


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


获取当前文章或文章ID的完整永久链接。


在插件或主题中,早在setup_Theme Action中就可以使用它。任何早期的使用,包括plugins_loaded,都会生成致命错误。


请注意,当在没有ID参数的文章页面(索引、存档等)上在循环之外使用时,它将返回循环中最后一篇文章的URL,而不是当前页面的永久链接。


参数说明:


$post,文章或文章ID。


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


函数源码:


function get_permalink( $post = 0, $leavename = false ) {
	$rewritecode = array(
		'%year%',
		'%monthnum%',
		'%day%',
		'%hour%',
		'%minute%',
		'%second%',
		$leavename ? '' : '%postname%',
		'%post_id%',
		'%category%',
		'%author%',
		$leavename ? '' : '%pagename%',
	);

	if ( is_object( $post ) && isset( $post->filter ) && 'sample' === $post->filter ) {
		$sample = true;
	} else {
		$post   = get_post( $post );
		$sample = false;
	}

	if ( empty( $post->ID ) ) {
		return false;
	}

	if ( 'page' === $post->post_type ) {
		return get_page_link( $post, $leavename, $sample );
	} elseif ( 'attachment' === $post->post_type ) {
		return get_attachment_link( $post, $leavename );
	} elseif ( in_array( $post->post_type, get_post_types( array( '_builtin' => false ) ), true ) ) {
		return get_post_permalink( $post, $leavename, $sample );
	}

	$permalink = get_option( 'permalink_structure' );

	$permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );

	if (
		$permalink &&
		! wp_force_plain_post_permalink( $post )
	) {

		$category = '';
		if ( strpos( $permalink, '%category%' ) !== false ) {
			$cats = get_the_category( $post->ID );
			if ( $cats ) {
				$cats = wp_list_sort(
					$cats,
					array(
						'term_id' => 'ASC',
					)
				);

				$category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );

				$category_object = get_term( $category_object, 'category' );
				$category        = $category_object->slug;
				if ( $category_object->parent ) {
					$category = get_category_parents( $category_object->parent, false, '/', true ) . $category;
				}
			}

			if ( empty( $category ) ) {
				$default_category = get_term( get_option( 'default_category' ), 'category' );
				if ( $default_category && ! is_wp_error( $default_category ) ) {
					$category = $default_category->slug;
				}
			}
		}

		$author = '';
		if ( strpos( $permalink, '%author%' ) !== false ) {
			$authordata = get_userdata( $post->post_author );
			$author     = $authordata->user_nicename;
		}

		$date = explode( ' ', str_replace( array( '-', ':' ), ' ', $post->post_date ) );

		$rewritereplace = array(
			$date[0],
			$date[1],
			$date[2],
			$date[3],
			$date[4],
			$date[5],
			$post->post_name,
			$post->ID,
			$category,
			$author,
			$post->post_name,
		);

		$permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) );
		$permalink = user_trailingslashit( $permalink, 'single' );

	} else { // If they're not using the fancy permalink option.
		$permalink = home_url( '?p=' . $post->ID );
	}

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


包含钩子:


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

apply_filters( 'post_link_category', WP_Term $cat, array $cats, WP_Post $post )

apply_filters( 'pre_post_link', string $permalink, WP_Post $post, bool $leavename )


使用举例:


function get_link_by_slug($slug, $type = 'post'){
  $post = get_page_by_path($slug, OBJECT, $type);
  return get_permalink($post->ID);
}


发表评论

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

客服 工单