1

i'have added thumbnail image to Rss of my blog and I'm using this code to display the rss in another website .

please guide me how to add image thumbnail to this code ...

see the result at : Pmanagers.org

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'http://pmanagers.org/blog/feed/' );
$maxitems = 0;
if ( ! is_wp_error( $rss ) ) :
    $maxitems = $rss->get_item_quantity( 6 ); 
    $rss_items = $rss->get_items( 0, $maxitems );
endif;
?>
<div class="headwrp">
<h4>Latest Blog posts</h4>
</div>
<ul>
<?php if ( $maxitems == 0 ) : ?>
        <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
    <?php else : ?>
        <?php // Loop through each feed item and display each item as a hyperlink. ?>
<div class=" owl-carousel">
        <?php foreach ( $rss_items as $item ) : ?>
<div class="item">
            <li id="news-single">

                <a href="<?php echo esc_url( $item->get_permalink() ); ?>"     target="_blank"
                    title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                <?php echo esc_html( $item->get_title() ); ?>
            </a>
        </li>
</div>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>
</div>
1

Try using the_post_thumbnail WP function, inside your <li id="news-single"> markup definition:

        <li id="news-single">

            <a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank" title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
            <?php echo esc_html( $item->get_title() ); ?>
            </a>

            <?php
            // This is the piece of code that you should add:
            // You first need to check if the post has a thumbnail assigned.
            if ( has_post_thumbnail() ) {
                the_post_thumbnail();
            } 
            ?>

        </li>

Hope it helps!

  • hello Capiedge , I've used this but won't work :( – 1001dlc May 30 '16 at 12:07
  • Are you sure that the posts has a post_thumbnail declared? – Capiedge May 30 '16 at 12:39
  • yes ,I'm sure you can see yourself at : pmanagers.org/blog/feed – 1001dlc May 30 '16 at 12:43
0

You can try this code

function get_first_image_url($html)
{
    if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
        return $matches[1];
    }
    else return 'url_of_default_image_if_post_has_no_img_tags.jpg';
}

and then add the line:

<?php echo '<img src="' .get_first_image_url($item->get_content()). '"/>'; ?>

you can find the source code here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.