1

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.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; ?>
2

After 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.org/support/topic-tag/get_description/

The Full code may you try.

<?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; ?>
| improve this answer | |
  • thanks so much...that did it. Now I need to figure out how to get rid of the unwanted html and style it correctly :) – jbuck Apr 10 '19 at 1:18
-1

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>
| improve this answer | |
  • Thanks. That was my first thought as that's how it is for the date and titIe, but when I tried that, it broke the whole thing...nothing showed up at all. – jbuck Apr 9 '19 at 11:24
  • Also, tiu say since I'm not using the default loop...should I be? Is that a better/safer way to do it? – jbuck Apr 9 '19 at 11:26
  • I edited my answer, try the new code and if you're getting any errors, please let me know. And about the default wordpress loop, no it's not better or safer, it all depends on your needs, when you don't want the wordpress default loop, you should make your own one. – Amirition Apr 9 '19 at 11:52

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.