我正在制作一个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' );
使用它:<div class="today-podcast" style="text-align: center;">[podcast]</div>
,它显示来自这里http://www.cpac.ca/tip-podcast/jwplayer.xml的全部内容
问题陈述:我想知道我应该在上面的wordpress短代码中做出什么改变,以便它只显示前两个项目或任何单个项目来自这里http://www.cpac.ca/tip-podcast/jwplayer.xml
最好先将返回的XML保存到文件中,然后循环返回取消设置。
<?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]);
}
您必须首先解析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专业人士,请原谅我的罪过,我是非常新的。欢迎并在评论中邀请对方法和语法进行更正。