WordPress函数is_single()判断当前页面类型是否为文章页

描述:

判断当前页面类型是否为文章页

用法:

<?php is_single($post); ?>

参数:

$post

(mixed) (可选) 文章ID, 文章标题,文章别名等

默认值: None

示例:

is_single();
// When any single Post page is being displayed.

is_single('17');
// When Post 17 (ID) is being displayed.

is_single(17);
// When Post 17 (ID) is being displayed. Integer parameter also works

源文件:

/**
 * Is the query for an existing single post?
 *
 * Works for any post type, except attachments and pages
 *
 * If the $post parameter is specified, this function will additionally
 * check if the query is for one of the Posts specified.
 *
 * @see is_page()
 * @see is_singular()
 *
 * @since 1.5.0
 *
 * @global WP_Query $wp_query
 *
 * @param mixed $post Post ID, title, slug, or array of such.
 * @return bool
 */
function is_single( $post = '' ) {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
		return false;
	}

	return $wp_query->is_single( $post );
}