如何限制来自 wordpress 简码的内容?
How to limit the content coming from wordpress shortcodes?
我正在开发一个 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 并生成所需媒体对象的数组,而不是直接访问 RSS 提要。
首先,您可以使用 PHPs xpath function 编写一个 xpath 查询并提取您要查找的字段。它允许您 select 并从 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,
那应该 return 只有您想要的元素。如果您想更进一步,请查看 this shortcode guide 以了解如何设置它以允许 wordpress 用户传递他们希望看到的对象数量! (我会把如何设置循环行为留给你,但我认为这很明显。)
PHP 亲,请见谅,我是新手。欢迎在评论中更正方法和语法。
最好先将返回的 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]);
}
我正在开发一个 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 并生成所需媒体对象的数组,而不是直接访问 RSS 提要。
首先,您可以使用 PHPs xpath function 编写一个 xpath 查询并提取您要查找的字段。它允许您 select 并从 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,
那应该 return 只有您想要的元素。如果您想更进一步,请查看 this shortcode guide 以了解如何设置它以允许 wordpress 用户传递他们希望看到的对象数量! (我会把如何设置循环行为留给你,但我认为这很明显。)
PHP 亲,请见谅,我是新手。欢迎在评论中更正方法和语法。
最好先将返回的 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]);
}