我正在尝试从RSS Feed中显示wordpress帖子中的内容。我希望能够将RSS URL输入到Wordpress帖子中,以便Feed内容可以在帖子之间更改。
我可以使用此代码创建所需的输出,并直接输入RSS网址,其中显示'ENTER RSS URL HERE'
<?php
$rss = new DOMDocument();
$rss->load('ENTER RSS URL HERE');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 6;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div class="product-item">';
echo '<p>'.$description.'</p>';
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '</div>';
}
?>
我还可以输出一个作为post meta值输入的URL,如下所示:
<?php echo get_post_meta($post->ID, 'RSS', true); ?>
我只是不确定如何将两者结合起来以便可以动态更改RSS URL。任何帮助将不胜感激。
答案 0 :(得分:0)
如果RSS-URL始终位于帖子元数据中,您只需使用:
$rss->load(get_post_meta($post->ID, 'RSS', true));
或者我的问题出了什么问题?
答案 1 :(得分:0)
您可以尝试以下
<?php
global $post;
$url = ((filter_var(get_post_meta($post->ID, 'RSS', true), FILTER_VALIDATE_URL)) ? get_post_meta($post->ID, 'RSS', true) : 'fallback url');
$rss = new DOMDocument();
$rss->load($url);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node)
{
$item = array(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 6;
for ($x = 0; $x < $limit; $x++)
{
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div class="product-item">';
echo '<p>' . $description . '</p>';
echo '<p><strong><a href="' . $link . '" title="' . $title . '">' . $title . '</a></strong><br />';
echo '</div>';
}
?>
答案 2 :(得分:0)
以下是我使用的最终代码,它似乎非常有效!
<?php
global $post;
$url = ((filter_var(get_post_meta($post->ID, 'RSS', true))) ? get_post_meta($post->ID, 'RSS', true) : 'ENTER FALL BACK URL HERE');
$rss = new DOMDocument();
$rss->load($url);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node)
{
$item = array(
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 6;
for ($x = 0; $x < $limit; $x++)
{
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div class="product-item">';
echo '<p>' . $description . '</p>';
echo '<p><strong><a href="' . $link . '" title="' . $title . '">' . $title . '</a></strong><br />';
echo '</div>';
}
?>