I'm new to coding and know next to nothing. I've done a ton research on the web, and I foudn the following code to import an RSS feed and place it in my template. The problem is that while I get 10 different titles, 10 different dates, 10 different images, I get the same excerpt for each item. Can someone please help me figure out what I'm doing wrong?
(I should mention that the code I found did nit include get_the_excerpt, I added that hoping to get the excerpt for each post, not the same one for all of them.)
Thanks.
<?php
$feed = fetch_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; ?>
I'm new to coding and know next to nothing. I've done a ton research on the web, and I foudn the following code to import an RSS feed and place it in my template. The problem is that while I get 10 different titles, 10 different dates, 10 different images, I get the same excerpt for each item. Can someone please help me figure out what I'm doing wrong?
(I should mention that the code I found did nit include get_the_excerpt, I added that hoping to get the excerpt for each post, not the same one for all of them.)
Thanks.
<?php
$feed = fetch_feed( 'http://example/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; ?>
Share
Improve this question
edited Apr 10, 2019 at 0:23
MikeNGarrett
2,6711 gold badge20 silver badges29 bronze badges
asked Apr 9, 2019 at 10:37
jbuckjbuck
135 bronze badges
2 Answers
Reset to default 2After Lots of Research and Testing, I got the answer.
the_excerpt and other custom function are not working because there is no any content tag On feed xml format. You just need to add this code instead of the_excerpt
<p><?php echo esc_html( $item->get_description() ); ?></p>
More you can Visit https://wordpress/support/topic-tag/get_description/
The Full code may you try.
<?php
$feed = fetch_feed( 'http://example/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; ?>
Since you're not using the default wordpress loop, you should write $item
before the functions that you wanna use in your loop, Same as you did for the permalink and the title.
So your code will be like:
//Display the post exerpt
<p><?php $item->the_excerpt(); ?></p>