prepend_attachment

函数
prepend_attachment ( $content )
参数
  • (string) $content
    Required:
返回值
  • (string)
定义位置
相关方法
wp_attachment_isis_attachmentwp_prepare_attachment_for_jswp_insert_attachmentwp_delete_attachment
引入
2.0.0
弃用
-

prepend_attachment函数是一个WordPress的函数,用来将一个文章或附件添加到一个给定数组的开头。它通常用于添加一个新的文章或附件到一个文章或附件的阵列中。

将附件包裹在内容之前的段落标签中。

function prepend_attachment( $content ) {
	$post = get_post();

	if ( empty( $post->post_type ) || 'attachment' !== $post->post_type ) {
		return $content;
	}

	if ( wp_attachment_is( 'video', $post ) ) {
		$meta = wp_get_attachment_metadata( get_the_ID() );
		$atts = array( 'src' => wp_get_attachment_url() );
		if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
			$atts['width']  = (int) $meta['width'];
			$atts['height'] = (int) $meta['height'];
		}
		if ( has_post_thumbnail() ) {
			$atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
		}
		$p = wp_video_shortcode( $atts );
	} elseif ( wp_attachment_is( 'audio', $post ) ) {
		$p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
	} else {
		$p = '<p class="attachment">';
		// Show the medium sized image representation of the attachment if available, and link to the raw file.
		$p .= wp_get_attachment_link( 0, 'medium', false );
		$p .= '</p>';
	}

	/**
	 * Filters the attachment markup to be prepended to the post content.
	 *
	 * @since 2.0.0
	 *
	 * @see prepend_attachment()
	 *
	 * @param string $p The attachment HTML output.
	 */
	$p = apply_filters( 'prepend_attachment', $p );

	return "$pn$content";
}

常见问题

FAQs
查看更多 >