is_local_attachment

函式
is_local_attachment ( $url )
引數
  • (string) $url URL to check
    Required:
返回值
  • (bool) True on success, false on failure.
定義位置
相關方法
is_attachmentis_locale_switchedwp_ajax_upload_attachmentwp_delete_attachmentwp_count_attachments
引入
2.0.0
棄用
-

is_local_attachment: 這個函式檢查一個給定的附件是否屬於當前網站。一個附件是一個附在文章或頁面上的檔案: 當你需要檢查一個檔案是否本地儲存在網站的媒體庫中時,這個函式很有用。

確定一個附件URI是否是本地的,是否真的是一個附件。

關於這個和類似的主題功能的更多資訊,請檢視《主題開發者手冊》中的{@link Conditional Tags}文章。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function is_local_attachment( $url ) {
if ( strpos( $url, home_url() ) === false ) {
return false;
}
if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) {
return true;
}
$id = url_to_postid( $url );
if ( $id ) {
$post = get_post( $id );
if ( 'attachment' === $post->post_type ) {
return true;
}
}
return false;
}
function is_local_attachment( $url ) { if ( strpos( $url, home_url() ) === false ) { return false; } if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) { return true; } $id = url_to_postid( $url ); if ( $id ) { $post = get_post( $id ); if ( 'attachment' === $post->post_type ) { return true; } } return false; }
function is_local_attachment( $url ) {
	if ( strpos( $url, home_url() ) === false ) {
		return false;
	}
	if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) {
		return true;
	}

	$id = url_to_postid( $url );
	if ( $id ) {
		$post = get_post( $id );
		if ( 'attachment' === $post->post_type ) {
			return true;
		}
	}
	return false;
}

常見問題

FAQs
檢視更多 >