wp_force_plain_post_permalink()
云策文档标注
概述
wp_force_plain_post_permalink() 函数用于判断文章是否应始终使用纯链接结构(plain permalink),基于文章状态、类型和用户权限等条件返回布尔值。
关键要点
- 函数返回布尔值,true 表示应使用纯链接结构,false 表示不应使用。
- 参数 $post 可选,默认为全局 $post,可以是文章 ID 或文章对象。
- 参数 $sample 可选,用于强制基于样本链接考虑,默认为 null。
- 逻辑检查包括:文章是否可公开查看、私有文章用户是否有读取权限、受保护文章是否在获取样本链接时。
- 如果文章不存在或状态/类型对象无效,默认返回 true。
代码示例
// Get the current post
$current_post = get_post();
// Checks if the post should use a simple permalink structure
$force_plain_permalink = wp_force_plain_post_permalink( $current_post );
// Use the result to customize the permalink output
if ( $force_plain_permalink ) {
echo trailingslashit( esc_url( get_permalink( $current_post ) ) ); // Add a slash at the end for a simple permalink structure
} else {
echo esc_url( get_permalink( $current_post ) ); // Uses the standard permalink structure
}
原文内容
Determine whether post should always use a plain permalink structure.
Parameters
$postWP_Post|int|nulloptional-
Post ID or post object. Defaults to global $post.
Default:
null $samplebool|nulloptional-
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'.Default:
null
Source
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;
}
Changelog
| Version | Description |
|---|---|
| 5.7.0 | Introduced. |
Skip to note 2 content
Rodrigo Vieira Eufrasio da Silva
// Get the current post $current_post = get_post(); // Checks if the post should use a simple permalink structure $force_plain_permalink = wp_force_plain_post_permalink( $current_post ); // Use the result to customize the permalink output if ( $force_plain_permalink ) { echo trailingslashit( esc_url( get_permalink( $current_post ) ) ); // Add a slash at the end for a simple permalink structure } else { echo esc_url( get_permalink( $current_post ) ); // Uses the standard permalink structure }