I’m building a site in WordPress in which I need to pull in an RSS feed from another site to display on a page. I’ve pulled it in, limited the number of items successfully. I’ve figured out how to truncate the description to limit it to a certain number of characters.
However if the post has images in the truncation gets messed up. I’ve tried many different ways of stripping out the ‘img’ tag and I haven’t been successful yet. Any guidance on the correct way to do this would be greatly appreciated. Here is the code for the blog section:
<?php
//fetch rss feed
$rss = fetch_feed('http://solar.greenstreetsolarpower.com/blog/rss.xml');
$rss->strip_htmltags('img');
// Checks that the object is created correctly
if ( ! is_wp_error( $rss ) ) :
//figure total items and limit to 3
$maxitems = $rss->get_item_quantity( 3 );
//build array of all times starting with first element
$rss_items = $rss->get_items( 0, $maxitems );
else:
echo 'RSS Feed is broken';
endif; ?>
<section>
<div class="blog-header">
<h2>Power Blog</h2>
</div>
<div class="row">
<?php foreach ( $rss_items as $item ) :
$excerpt = $item->get_description();
$excerpt = substr($excerpt, 0, 120);
?>
<div class="small-12 medium-4 columns blog-excerpt">
<h3><?php echo esc_html( $item->get_title() ); ?></h3>
<p style="font-size: .8em !important;"><?php echo $excerpt; ?></p>
<a href="<?php echo esc_url( $item->get_permalink() ); ?>" title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">Continue</a>
</div>
<?php endforeach; ?>
</div>
</section>
Thanks for any help!
See: http://simplepie.org/wiki/reference/simplepie/strip_htmltags#description for reference to the method you are using.
strip_htmltags method expects the tags to be passed in an array and may not know how to handle an argument that is a single string.
Changing line 4 to
$rss->strip_htmltags($tags = array('img'));
should work.Hope this helps!