【问题标题】:How to limit the content coming from wordpress shortcodes?如何限制来自 wordpress 短代码的内容?
【发布时间】:2019-08-22 02:23:39
【问题描述】:

我正在开发一个 wordpress 简码,我想在其中限制来自 xml 的内容。

我用来创建 wordpress 简码的代码是:

function podcast_func( $content = null ){
    ob_start();
    ?>
<script src="https://content.jwplatform.com/libraries/FZ8yNTef.js"></script>
<center><div id="podcast" align="center"></div></center> 
<script> 
var PodcastplayerInstance = jwplayer("podcast"); 
PodcastplayerInstance.setup({ 
  playlist: "http://www.cpac.ca/tip-podcast/jwplayer.xml", 
  androidhls: true, 
  preload: "auto", 
  height: 200, 
  width: 400,
  visualplaylist:false,
  stretching: "fill",
    "plugins": {
        "http://www.cpac.ca/tip-podcast/listy.js":{},
        'viral-2': {'oncomplete':'False','onpause':'False','functions':'All'}
    }
});
</script> 
<?PHP
    return ob_get_clean();
}
add_shortcode( 'podcast', 'podcast_func' );


在使用这个:&lt;div class="today-podcast" style="text-align: center;"&gt;[podcast]&lt;/div&gt; 时,它会从这里显示全部内容 http://www.cpac.ca/tip-podcast/jwplayer.xml

问题陈述: 我想知道我应该在上面的 wordpress 短代码中进行哪些更改,以便它只显示 第一个两个项目任何单个项目 从这里http://www.cpac.ca/tip-podcast/jwplayer.xml

【问题讨论】:

  • 可以在您的站点上添加一个 xml 解析器,该解析器获取远程数据并删除除第一个 2 之外的所有数据。然后使用服务器上的端点传递到 setup
  • @charlietfl 我想知道你是否可以在回答中让我知道我必须做什么。
  • @charlietfl 你在吗?
  • 将需要您进行研究.....如何在 php 中使用 cURL 获取远程 xml 数据,如何使用 php 解析 xml 等

标签: javascript php wordpress xml-parsing jwplayer


【解决方案1】:

您必须先解析 XML 并生成所需媒体对象的数组,而不是直接访问 RSS 提要。

首先,您可以使用PHPs xpath function 编写一个xpath 查询并提取您要查找的字段。它允许您从 XML 文件中选择和提取字段。检索 XML 查询可能如下所示:

    $context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
    $url = 'http://www.cpac.ca/tip-podcast/jwplayer.xml';

    $xml = file_get_contents($url, false, $context);
    $xml = simplexml_load_string($xml);
    $itemarray = $xml->xpath("/rss/channel/item[1]");

那么我们在这里做什么?我们正在创建一个仅包含 XML 文件中的第一项的数组。该数组将如下所示:

    Array
    (
    [0] => Item
         (
         [title] => April 3, 2019
         [description] => Jody Wilson-Raybould...
         [jwplayer:image] => {image URL}
         [jwlplayer:source] => SimpleXMLElement Object
         )
    )

此时,您可以将数组解析为 media objects 以插入到您的构造中:

$playlist[] = [
     "file" => $itemarray[0][jwplayer:source]->['file'],
     "image" => $itemarray[0][jwplayer:image],
     "description" => $itemarray[0][description],
     "title" => $itemarray[0][title]
     ];

完成后,您可以照常将媒体对象数组传递给 API:

    PodcastplayerInstance.setup({ 
      playlist: <?php echo json_encode($playlist); ?>, 
      androidhls: true, 

这应该只返回你想要的元素。如果您想更进一步,请查看this shortcode guide 了解如何设置它以允许 wordpress 用户传递他们希望看到的对象数量! (如何设置循环行为由您决定,我认为这很明显。)

PHP 专业人士,请原谅我的罪过,我对此很陌生。欢迎并邀请在 cmets 中对方法和语法进行更正。

【讨论】:

  • playlist: json_encode($playlist) 应该是 playlist: &lt;?php echo json_encode($playlist); ?&gt; ..
  • 这实际上给我带来了一个有趣的问题!我认为他不希望它被回显,因为他正在编写一个简码(wordpress 要求您返回简码的输出,而不是回显)。但是现在您指出了,由于 ob_start 函数之后的 ?> ,该代码实际上不起作用。他怎么能把它包含在他的函数的返回中?在这种情况下 echo 会这样做吗?
  • 他正在使用输出缓冲,所以回显没有问题。你试过他的代码吗?他使用ob_get_clean()返回输出。
  • 我还没有运行它,没有。我还没有搞砸输出缓冲(我是新手!:D)。
  • @PhilippeHaussmann 你在吗?我在玩你的代码,我有一些问题。
【解决方案2】:

最好先将返回的 XML 保存到文件中,然后再循环返回 unset。

<?php
  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => "http://www.cpac.ca/tip-podcast/jwplayer.xml",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
      "cache-control: no-cache",
      "postman-token: 28025ee8-1e82-ce60-f6ae-f401118baa1c"
    ),
  ));

  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);

  if ($err) {
    echo "cURL Error #:" . $err;
  } else {
    $fp = fopen(ABSPATH.'jwp.xml', 'w');
    fwrite($fp, $response);
    fclose($fp);
  }

  $xml = simplexml_load_file(ABSPATH.'jwp.xml');

  for($i = count($xml->channel->item); $i >= 2; $i--){
    unset($xml->channel->item[$i]);
  }

  $xml->saveXML(ABSPATH.'jwp.xml');

  ?>
  <script src="https://content.jwplatform.com/libraries/FZ8yNTef.js"></script>
  <center><div id="podcast" align="center"></div></center> 
  <script> 
  var PodcastplayerInstance = jwplayer("podcast"); 
  PodcastplayerInstance.setup({ 
    playlist: "<?php echo site_url(); ?>/jwp.xml", 
    androidhls: true, 
    preload: "auto", 
    height: 200, 
    width: 400,
    visualplaylist:false,
    stretching: "fill",
      "plugins": {
          "http://www.cpac.ca/tip-podcast/listy.js":{},
          'viral-2': {'oncomplete':'False','onpause':'False','functions':'All'}
      }
  });
  </script> 

如果您只需要第二个或第三个元素,请使用以下代码更新上述代码

for($i = count($xml->channel->item); $i >= 3; $i--){
  unset($xml->channel->item[$i]);
}

for($i = 0; $i < count($xml->channel->item); $i++){
  unset($xml->channel->item[0]);
}

【讨论】:

  • 我粘贴了你的代码here 它说找不到文件。
  • copy 函数应该创建一个名为 jwp.xml 的新 xml 文件。如果文件未创建,请使用function_exists 检查copy 函数是否存在。另一个原因可能是该函数无法在服务器中创建文件。
  • 我粘贴了这段代码if (function_exists('copy')) { echo "IMAP functions are available.&lt;br /&gt;\n"; } else { echo "IMAP functions are not available.&lt;br /&gt;\n"; },上面写着here
  • 我猜它无法在服务器上创建文件。
  • 你在吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-11
  • 2019-03-23
  • 2019-10-29
  • 2012-06-20
  • 2014-01-04
相关资源
最近更新 更多