WordPress 视频功能允许通过短代码 [video] 嵌入和播放视频文件,支持多种格式如 mp4、webm 等。文档详细介绍了短代码的使用方法、选项(如循环、自动播放、海报图)以及样式自定义。
// 基本短代码示例
[video src="pepper.mp4"]
// 在模板文件中使用 do_shortcode()
$video_file = get_template_directory_uri() . "/videos/pepper.mp4";
echo do_shortcode( '[video mp4=' . $video_file . ']' );
// 带循环和自动播放选项
echo do_shortcode( '[video mp4=' . $video_file . ' loop="on" autoplay=1]' );
// 设置海报图和尺寸
echo do_shortcode( '[video mp4=' . $video_file . ' poster=' . get_template_directory_uri() . '/images/album_cover.jpg]' );
echo do_shortcode( '[video mp4=' . $video_file . ' width=320 height=240]' );The WordPress video feature allows you to embed video files and play them back using a simple shortcode . Supported file types are mp4, m4v, webm, ogv, wmv and flv.
Following shortcode displays video player that loads pepper.mp4 file:
To use the shortcode in the template file, use the do_shortcode() function. If the video file is stored in in your theme directory, get the file url directly using get_template_directory_uri() or get_stylesheet_uri() :
$video_file = get_template_directory_uri() . "/videos/pepper.mp4";
echo do_shortcode( '' );
The following video player will be loaded.
The shortcode video has the same option with audio. Refer to the related section for the loop and autoplay options.
The following example starts playing the video immediately after the page load and loops:
echo do_shortcode( '' );
The following basic options are supported:
Defines image to show as placeholder before the media plays.
The following same code takes album_cover.jpg stored in (theme directory)/images folder as the initial image:
echo do_shortcode( '' );
Defines height of the media. Value is automatically detected on file upload. When you omit this option, the media file height is used.
Defines width of the media. Value is automatically detected on file upload. When you omit this option, the media file width is used.
The following example will load the audio player with 320 pixels width and 240 pixels height:
echo do_shortcode( '' );
If you want to change look & feel of video player from stylesheet, you can target the class name of “wp-video-shortcode”. If you want to show the audio player like above in 320 x 240 size, insert following code into your stylesheet:
.wp-video-shortcode {
height: 240px;
width: 320px;
}
For more technical details such as internal library that enables this function, refer to