問題描述

我正在為一個帶有影片教程的網站製作一個 WordPress 主題。我想將內容中嵌入的影片 (使用 oEmbed) 放在 div div 中。

一個例子

全部內容 (the_content()的輸出) 是這樣的:

<p><iframe src="http://player.vimeo.com/video/0000000" width="900" height="506" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
<p>This is an Test of a tutorial. Bla bla bla</p>

我想得到:

<div id="video">
<iframe src="http://player.vimeo.com/video/0000000" width="900" height="506" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
<div id="content">
<p>This is an Test of a tutorial. Bla bla bla</p>
</div>

最佳解決方案

embed_oembed_html 過濾器在輸出 oEmbed 資源的 HTML 之前執行,因此您可以掛接到這個過濾器中,並將輸出包裝在 div 中,如下所示。我不能想到一個簡單的方法來包裝其他內容。

add_filter('embed_oembed_html', 'my_embed_oembed_html', 99, 4);
function my_embed_oembed_html($html, $url, $attr, $post_id) {
  return '<div id="video">' . $html . '</div>';
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。