How to add list of other blogs, with latest posts from those blogs, to a single page of my website?

wordpress.stackexchange https://wordpress.stackexchange.com/questions/371098

  •  20-04-2021
  •  | 
  •  

Question

I would like a self-updating list like the one at the bottom left of this page

https://aclerkofoxford.blogspot.com/p/old-english-wisdom.html

added to just one page of my website. I don't care how it's done, although I would prefer custom code I can add myself, rather than using a plugin. I have searched on this site for hints, but most results seem to be about listing one's own blog rather than others. Many thanks.

Was it helpful?

Solution

There's a function called fetch_feed, and an example of code to write custom code to do this for a single site contributed in the comments on that page. If you're comfortable writing a bit more custom code, it would be easy to alter this to pull from several feeds, and limit them to e.g. 1 or 2 latest posts from each site.

Note This approach is fine for a low traffic site, but for a busy site you will be hitting these RSS feeds on every pageload to your site, so you may want to think about implementing caching.

Here's the code to fetch and render a single RSS feed, you should be able to work from this. Refer to the fetch_feed documentation and this pages with more descrtiption of the fields in RSS for more detail

 
<?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://example.com/rss/feed/goes/here' );
 
$maxitems = 0;
 
if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly
 
    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity( 5 ); 
 
    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items( 0, $maxitems );
 
endif;
?>
 
<ul>
    <?php if ( $maxitems == 0 ) : ?>
        <li><?php _e( 'No items', 'wpdocs_textdomain' ); ?></li>
    <?php else : ?>
        <?php // Loop through each feed item and display each item as a hyperlink. ?>
        <?php foreach ( $rss_items as $item ) : ?>
            <li>
                <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'wpdocs_textdomain' ), $item->get_date('j F Y | g:i a') ); ?>">
                    <?php echo esc_html( $item->get_title() ); ?>
                </a>
            </li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top