我正在尝试使用:
add_filter( \'the_content\', \'filter_function_name\' );
在我的帖子中找到链接到的所有锚定标签。mp3文件,然后用一个jplayer实例来替换它们,该实例将播放该文件,而不仅仅是显示要作为文件的链接。
因此,我试图将由\\u内容输出的所有锚标记替换为以下html和js的实例:
html:
<div id="jquery_jplayer_#uniqueid" class="jp-jplayer"></div>
<div id="jp_container_#uniqueid" class="jp-audio">
<div class="jp-type-single">
<div class="jp-gui jp-interface">
<ul class="jp-controls">
<li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
<li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
<li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
<li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
<li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
<li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
</ul>
<div class="jp-progress">
<div class="jp-seek-bar">
<div class="jp-play-bar"></div>
</div>
</div>
<div class="jp-volume-bar">
<div class="jp-volume-bar-value"></div>
</div>
<div class="jp-time-holder">
<div class="jp-current-time"></div>
<div class="jp-duration"></div>
<ul class="jp-toggles">
<li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
<li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
</ul>
</div>
</div>
<div class="jp-title">
<ul>
<li>Music Player</li>
</ul>
</div>
<div class="jp-no-solution">
<span>Update Required</span>
To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
</div>
</div>
</div>
Javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#jquery_jplayer_#uniqueid").jPlayer({
ready: function () {
$(this).jPlayer("setMedia", {
m4a: "#the_url_from_the_anchor_tag",
}) //.jPlayer("play");
},
swfPath: "js/jplayer",
supplied: "m4a, mp3, oga",
wmode:"window",
cssSelectorAncestor: \'#jp_container_#uniqueid\'
});
});
</script>
非常感谢您的帮助。谢谢
最合适的回答,由SO网友:s_ha_dum 整理而成
您需要在the_content
并更改标记。这应替换的标记<a>
URL以结尾的标记.mp3
, 你在评论中特别提到了这一点。
function replace_audio_link_cb_105555($match) {
if (!empty($match[1]) && \'.mp3\' == substr($match[1],-4)) {
$your_long_block_of_markup = \'<div id="jquery_jplayer_#uniqueid" class="jp-jplayer"></div>
<div id="jp_container_#uniqueid" class="jp-audio">... and so on ...</div>\';
$your_javascript = \'<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#jquery_jplayer_#uniqueid").jPlayer({
ready: function () {
jQuery(this).jPlayer("setMedia", {
m4a: "#the_url_from_the_anchor_tag",
}) //.jPlayer("play");
},
swfPath: "js/jplayer",
supplied: "m4a, mp3, oga",
wmode:"window",
cssSelectorAncestor: "\'.$match[1].\'"
});
});
</script>\';
$match[0] = $your_long_block_of_markup.$your_javascript;
}
return $match[0];
}
function replace_audio_link_105555($content) {
$pattern = \'|<a.*?href="(.*)".*>?.*?(?:</a>)?|\';
$content = preg_replace_callback($pattern,\'replace_audio_link_cb_105555\',$content);
return $content;
}
add_filter(\'the_content\',\'replace_audio_link_105555\');
如果是我,我会想办法让javascript更加灵活,这样我就可以注册并排队,而不是为每个音频文件单独打印。