本文档介绍了 WordPress 中音频嵌入功能,通过 [audio] 短码直接播放音频文件,支持多种格式如 mp3、ogg、wma、m4a 和 wav。文档涵盖了短码的基本用法、循环与自动播放选项、自定义样式方法以及相关技术参考。
// 基本短码示例
[[audio src="music.mp3"]]
// 在模板文件中使用 do_shortcode
$music_file = get_template_directory_uri() . "/sounds/music.mp3";
echo do_shortcode('[[audio mp3=' . $music_file . ']]');
// 带循环和自动播放选项的短码
echo do_shortcode('[[audio mp3=' . $music_file . ' loop = "on" autoplay = 1]]');
// 自定义样式 CSS
.wp-audio-shortcode {
width: 50%;
}You can directly embed audio files and play them back using a simple shortcode . Supported file types are mp3, ogg, wma, m4a and wav.
Following shortcode displays audio player that loads music.mp3 file:
[audio src="music.mp3"]
To use the shortcode from template file, use do_shortcode function. When music.mp3 file was stored in (theme_directory)/sounds directory, insert following code into your template file:
$music_file = get_template_directory_uri() . "/sounds/music.mp3";
echo do_shortcode('[audio mp3=' . $music_file . ']');
The shortcode creates the audio player as shown in the screenshot below.

The following basic options are supported:
Allows for the looping of media.
Causes the media to automatically play as soon as the media file is ready.
The following example starts playing music immediately after the page load and loops.
echo do_shortcode('[audio mp3=' . $music_file . ' loop = "on" autoplay = 1]');
If you want to change the look & feel of audio player, you can do so by targeting the default class name of “wp-audio-shortcode”. If you insert following code into your style.css, half width of audio player will be displayed.
.wp-audio-shortcode {
width: 50%;
}
For more technical details such as the internal library that enables this function, refer to