has_excerpt()通过ID判断文章是否设置了摘要

目录

描述

Whether the current post has an excerpt.

has_excerpt() 描述

用法

<?php has_excerpt$id ); ?>

has_excerpt() 用法

参数

$id

(integer) (可选) Post ID

默认值: Current post ID

has_excerpt() 参数

返回值

(boolean) 

True if the post has an excerpt, false if it does not.

has_excerpt() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
// Get $post if you're inside a function
global $post;

if ( has_excerpt( $post->ID ) ) {
    // This post has excerpt
} else {
    // This post has no excerpt
}
?>

Other Use

When you need to hide the auto displayed excerpt and only display your post's excerpts.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php if ( ! has_excerpt() ) {
      echo '';
} else { 
      the_excerpt();
}

Replace auto excerpt for a text or code.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php if ( ! has_excerpt() ) {?>
    <!-- you text or code -->
<?php } ?>

has_excerpt() 示例

源文件

has_excerpt() 函数的代码位于 wp-includes/post-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Whether post has excerpt.
 *
 * @since 2.3.0
 *
 * @param int|WP_Post $id Optional. Post ID or post object.
 * @return bool
 */
function has_excerpt( $id = 0 ) {
	$post = get_post( $id );
	return ( !empty( $post->post_excerpt ) );
}

has_excerpt() 源文件

相关