WordPress函数:is_singular 是否是文章(任何类型)页面

江河/ 2023年09月09日/ WordPress/ 浏览 546

函数原型:


is_singular( string|string[] $post_types = '' ): bool


确定查询是否针对任何帖子类型(帖子、附件、页面、自定义帖子类型)的现有单个帖子。


如果指定了$post_types参数,此函数将另外检查查询是否针对指定的Posts类型之一。


有关此主题函数和类似主题函数的更多信息,请参阅主题开发人员手册中的条件标记文章。


函数源码:


function is_singular( $post_types = '' ) {
	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.0' );
		return false;
	}

	return $wp_query->is_singular( $post_types );
}


使用举例:


if ( is_singular() ) {
  // show adv. #1
} else {
  // show adv. #2
}


发表评论

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

客服 工单