wp_force_plain_post_permalink

函数
wp_force_plain_post_permalink ( $post = null, $sample = null )
参数
  • (WP_Post|int|null) $post Optional. Post ID or post object. Defaults to global $post.
    Required:
    Default: null
  • (bool|null) $sample Optional. Whether to force consideration based on sample links. If omitted, a sample link is generated if a post object is passed with the filter property set to 'sample'.
    Required:
    Default: null
返回值
  • (bool) Whether to use a plain permalink structure.
定义位置
相关方法
get_post_permalinkpost_permalinkwp_ajax_get_permalinkwp_ajax_sample_permalinkprev_post_rel_link
引入
5.7.0
弃用
-

wp_force_plain_post_permalink。这个过滤器用来强迫WordPress对文章的URL使用普通的固定结构,而不是包含文章名称的默认结构。它通常被用来提高有大量文章的网站的性能。

决定文章是否应该总是使用普通的固定结构。

function wp_force_plain_post_permalink( $post = null, $sample = null ) {
	if (
		null === $sample &&
		is_object( $post ) &&
		isset( $post->filter ) &&
		'sample' === $post->filter
	) {
		$sample = true;
	} else {
		$post   = get_post( $post );
		$sample = null !== $sample ? $sample : false;
	}

	if ( ! $post ) {
		return true;
	}

	$post_status_obj = get_post_status_object( get_post_status( $post ) );
	$post_type_obj   = get_post_type_object( get_post_type( $post ) );

	if ( ! $post_status_obj || ! $post_type_obj ) {
		return true;
	}

	if (
		// Publicly viewable links never have plain permalinks.
		is_post_status_viewable( $post_status_obj ) ||
		(
			// Private posts don't have plain permalinks if the user can read them.
			$post_status_obj->private &&
			current_user_can( 'read_post', $post->ID )
		) ||
		// Protected posts don't have plain links if getting a sample URL.
		( $post_status_obj->protected && $sample )
	) {
		return false;
	}

	return true;
}

常见问题

FAQs
查看更多 >