【问题标题】:How to get the first embed content of a post如何获取帖子的第一个嵌入内容
【发布时间】:2014-01-12 09:03:52
【问题描述】:

我正在尝试从帖子中获取第一个嵌入网址。 我知道 wordpress 会自动嵌入它们,但找不到一种方法来获取 嵌入。

                 $urls = array();
                 $p = get_post();
                 preg_match('@https?://(www.)?(youtube|vimeo)\.com/(watch\?v=)?([a-zA-Z0-9]+)@im', $p->post_content, $urls);
                 echo wp_oembed_get( $urls[0] );

【问题讨论】:

    标签: php wordpress youtube embed vimeo


    【解决方案1】:

    自 Wordpress 2.9 起,已包含 wp_oembed_get 函数。

    例子:

    <?php $embed_code = wp_oembed_get($url); ?>
    

    这里是函数的链接,用于获取嵌入:

    http://codex.wordpress.org/Function_Reference/wp_oembed_get

    【讨论】:

      【解决方案2】:

      这个函数将返回第一个[embed]短代码:

      function sofa_get_embed_shortcode($content) {
          preg_match('/\[embed(.*)](.*)\[\/embed]/', $content, $matches);
          return $matches;
      }
      

      $post-&gt;post_content 作为$content 变量传递,您将收到一组找到的[embed] 短代码。

      【讨论】:

      • WP 是否将 youtube... 替换为嵌入标签?我希望用户只粘贴 URL。
      猜你喜欢
      • 2021-05-19
      • 2012-01-31
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多