WordPress函数:is_attachment 确定查询是否针对现有附件页

江河/ 2023年09月08日/ WordPress/ 浏览 456

函数原型:


is_attachment( int|string|int[]|string[] $attachment = '' ): bool


确定查询是否针对现有附件页。


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


另请参阅:is_singular()


有关附件的更具体检查,请参阅:wp_attachment_is_image()


函数源码:


function is_attachment( $attachment = '' ) {
	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_attachment( $attachment );
}


使用举例:


<?php
if ( is_attachment() ) {
	// show adv. #1
} else {
	// show adv. #2
}


发表评论

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

客服 工单