atom_enclosure

函式
atom_enclosure ( No parameters )
定義位置
相關方法
do_encloserss_enclosuredo_all_enclosuresget_enclosedupdate_core
引入
2.2.0
棄用
-

atom_enclosure: 這個函式用來給一個RSS feed專案新增Atom外殼。它需要兩個引數:第一個是圍欄的URL,第二個是圍欄的大小(位元組)。

顯示當前文章的atom外殼。

使用全域性$post來檢查該文章是否需要密碼,以及使用者是否有該文章的密碼。如果沒有,那麼在顯示之前會返回。

還使用函式get_post_custom()來獲取文章的’enclosure’後設資料欄位,並解析該值以顯示附件。圍欄由帶有URI和其他屬性的HTML連結標籤組成。

function atom_enclosure() {
	if ( post_password_required() ) {
		return;
	}

	foreach ( (array) get_post_custom() as $key => $val ) {
		if ( 'enclosure' === $key ) {
			foreach ( (array) $val as $enc ) {
				$enclosure = explode( "n", $enc );

				$url    = '';
				$type   = '';
				$length = 0;

				$mimes = get_allowed_mime_types();

				// Parse URL.
				if ( isset( $enclosure[0] ) && is_string( $enclosure[0] ) ) {
					$url = trim( $enclosure[0] );
				}

				// Parse length and type.
				for ( $i = 1; $i <= 2; $i++ ) {
					if ( isset( $enclosure[ $i ] ) ) {
						if ( is_numeric( $enclosure[ $i ] ) ) {
							$length = trim( $enclosure[ $i ] );
						} elseif ( in_array( $enclosure[ $i ], $mimes, true ) ) {
							$type = trim( $enclosure[ $i ] );
						}
					}
				}

				$html_link_tag = sprintf(
					"<link href="%s" rel="enclosure" length="%d" type="%s" />n",
					esc_url( $url ),
					esc_attr( $length ),
					esc_attr( $type )
				);

				/**
				 * Filters the atom enclosure HTML link tag for the current post.
				 *
				 * @since 2.2.0
				 *
				 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
				 */
				echo apply_filters( 'atom_enclosure', $html_link_tag );
			}
		}
	}
}

常見問題

FAQs
檢視更多 >