我对编码一无所知。我在web上做了大量的研究,并编写了以下代码来导入RSS提要并将其放置在我的模板中。问题是,虽然我得到了10个不同的标题、10个不同的日期、10张不同的图片,但我得到的每个项目都是相同的摘录。有人能帮我找出我做错了什么吗?
(我应该提到的是,我找到的代码没有包含get\\u节选,我补充说希望每个帖子都能得到节选,而不是所有帖子都是一样的。)
谢谢
<?php
$feed = fetch_feed( \'http://example.com/feed/\' );
if (!is_wp_error( $feed ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 10.
$maxitems = $feed->get_item_quantity(10);
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $feed->get_items(0, $maxitems);
endif;
?>
<?php if ($maxitems == 0) echo \'<li>No items.</li>\';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<div>
<?php
//Use regular expressions to find all images in the post
$output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\', $item->get_content(), $matches);
//Grab the first image to use as a thumbnail
$first_img = $matches [1][0];
//If an image exists, display it
if($first_img) {echo \'<img src="\'.$first_img.\'" alt="\'.$item->get_title().\'" />\';}
?>
//Display the post title as a link inside an <h5> tag
<h5><a href=\'<?php echo esc_url( $item->get_permalink() ); ?>\'
title=\'<?php echo \'Posted \'.$item->get_date(\'j F Y | g:i a\'); ?>\'>
<?php echo esc_html( $item->get_title() ); ?></a></h5>
//Display the item\'s publishing date
<p><?php echo $item->get_date(\'n/d/Y\'); ?></p>
</div>
//Display the post exerpt
<p><?php echo get_the_excerpt(); ?></p>
<?php endforeach; ?>
最合适的回答,由SO网友:Pawan Kumar 整理而成
经过大量的研究和测试,我得到了答案。
\\u摘录和其他自定义函数不起作用,因为提要xml格式上没有任何内容标记。您只需要添加此代码而不是\\u摘录
<p><?php echo esc_html( $item->get_description() ); ?></p>
您可以访问的更多内容
https://wordpress.org/support/topic-tag/get_description/您可以尝试完整的代码。
<?php
$feed = fetch_feed( \'http://example.com/rss/feed/goes/here\' );
if (!is_wp_error( $feed ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 10.
$maxitems = $feed->get_item_quantity(10);
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $feed->get_items(0, $maxitems);
endif;
?>
<?php if ($maxitems == 0) echo \'<li>No items.</li>\';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<div>
<?php
//Use regular expressions to find all images in the post
$output = preg_match_all(\'/<img.+src=[\\\'"]([^\\\'"]+)[\\\'"].*>/i\', $item->get_content(), $matches);
//Grab the first image to use as a thumbnail
$first_img = $matches [1][0];
//If an image exists, display it
if($first_img) {echo \'<img src="\'.$first_img.\'" alt="\'.$item->get_title().\'" />\';}
?>
<h5><a href=\'<?php echo esc_url( $item->get_permalink() ); ?>\'
title=\'<?php echo \'Posted \'.$item->get_date(\'j F Y | g:i a\'); ?>\'>
<?php echo esc_html( $item->get_title() ); ?></a></h5>
<p><?php echo $item->get_date(\'n/d/Y\'); ?></p>
</div>
<p><?php echo esc_html( $item->get_description() ); ?></p>
<?php endforeach; ?>