Skip to main content

Display WordPress Posts on another WordPress Blog

Following on from the post Display Your WordPress Recent Posts on a Static Page which was used to display your WordPress posts on a non-WP website on the same server. I have had several people contacting me on how to display WordPress posts on another WordPress blog. This tutorial will show you how to display your posts, associated post thumbnail images and a snippet of the content from one blog to another by fetching the data from your RSS feed using the SimplePie RSS parser which is included in the WordPress installation.


Display WordPress Posts

Enable Post Thumbnails in WordPress & RSS Feeds

To be able to display the post thumbnails you need to enable or check that your theme supports thumbnails and also enable post thumbnails to show up in your RSS feeds.

To display your post thumbnails in your feeds you need to add the following to your theme’s functions.php

<?php
//add post thumbnails to RSS images
function cwc_rss_post_thumbnail($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID) .
        '</p>' . get_the_excerpt();
    }

    return $content;
}
add_filter('the_excerpt_rss', 'cwc_rss_post_thumbnail');
add_filter('the_content_feed', 'cwc_rss_post_thumbnail');
?>

Whilst functions.php is open check for or add the following

add_theme_support( 'post-thumbnails' );

Don’t forget to add the thumbnails to your posts!

Code to Display WordPress Posts

Open up your theme template and add the following snippet of code

<?php 
$rss = fetch_feed('https://www.worldoweb.co.uk/feed');


if (!is_wp_error( $rss ) ) : 
	
    $maxitems = $rss->get_item_quantity(5); 
    $rss_items = $rss->get_items(0, $maxitems); 
endif;
?>
<?php function get_first_image_url($html)
		{
			if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
			return $matches[1];
			}
		}
?>	
 <?php
function shorten($string, $length)
{
    $suffix = '&hellip;';

$short_desc = trim(str_replace(array("/r", "/n", "/t"), ' ', strip_tags($string)));
    $desc = trim(substr($short_desc, 0, $length));
    $lastchar = substr($desc, -1, 1);
    	if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix='';
					$desc .= $suffix;
 		return $desc;
}
?>
<ul class="rss-items" id="wow-feed">
    <?php 
    	if ($maxitems == 0) echo '<li>No items.</li>';
    	else 
    	foreach ( $rss_items as $item ) : ?>
    <li class="item">
    	<span class="rss-image">
    		<?php echo '<img src="' .get_first_image_url($item->get_content()). '"/>'; ?>
    	</span>
        <span class="data">
        	<h5><a href='<?php echo esc_url( $item->get_permalink() ); ?>' title='<?php echo esc_html( $item->get_title() ); ?>'> <?php echo esc_html( $item->get_title() ); ?></a></h5> 
			<span class="date-image">&nbsp;</span><small><?php echo $item->get_date('F Y'); ?> </small>
			<span class="comment-image">&nbsp;</span><small><?php $comments = $item->get_item_tags('https://purl.org/rss/1.0/modules/slash/', 'comments');?><?php $number = $comments[0]['data']; ?>
				<?php if ($number == '1'){ echo $number."&nbsp;". "Comment"; } else {echo $number. "&nbsp;"."Comments";}?></small>
			<p><?php echo shorten($item-> get_description(),'150');?></p>
        </span>
    </li>
    <?php endforeach; ?>
</ul>

Now we need to edit the above snippet to suit your needs. I have highlighted the lines that need to be changed. I recommend that you get the script running before making any other changes and always test it in a development environment and not in a production environment.

Line 2 Change this to the URL of the feed you want to fetch.

Line 7 Gets the latest 5 posts. This can be changed to suit your needs.

Line 11-17 This function grabs the first image that is contained in the feed.

Line 18-29 This function is used to shorten the description and not display the full blog post.

Line 30 This is the start of our output which is in the form of an unordered list.

Line 36-38 This is where our image from the post thumbnails feature should display. Please note that linking to an external blog which is not your own may result in no images being displayed.

Line 41 get_date(‘F Y’); This will display the date of the published content. This can be changed to suit your requirements. Read Formatting Date and Time to learn more.

Line 42-43 grabs the amount of comment(s) the post has received. This may not show up as it depends on your RSS feed structure.

Line 44 shorten($item-> get_description(),’150′) uses the function shorten (Line 19-29) to display the blog post to a maximum of 150 characters. This can be changed to suit your requirements.

Add the CSS

Now it’s time to spice up the content with a little CSS. Open up your stylesheet, normally style.css in WordPress. Feel free to change any of the CSS to suit your requirements.

#wow-feed {
    background: #FFFFFF;
    border: 1px solid #AFAFB0;
    width: 600px;
    margin: 10px 0;
    font-size: 0.8em;
}
#wow-feed  li { list-style: none; }
#wow-feed .rss-image img {
    width: 100px;
    height: 100px;
    padding: 8px;
    border: solid 1px #eee;
}
#wow-feed .rss-image { width: 30%; }
#wow-feed .item {
    border-bottom: 1px solid #AFAFB0;
    padding: 10px;
}
#wow-feed .data {
    display: inline-block;
    margin-left: 2%;
    vertical-align: top;
    width: 70%;
}
#wow-feed .data h5 { font-weight: bold; }
#wow-feed .data small {
    color: #8F90CB;
    font-size: 0.9em;
    margin-right: 10%;
}
#wow-feed .comment-image {
    background: url("images/comments.png");
    height: 16px;
    width: 16px;
    vertical-align: middle;
    display: inline-block;
    margin-right: 2%;
}
#wow-feed .date-image {
    background: url("images/date.png");
    height: 16px;
    width: 16px;
    vertical-align: middle;
    display: inline-block;
    margin-right: 2%;
}

All you need to do now is to add the images to your theme images folder. I have packed the tutorial and image files into a handy zip file, available below. If the comments and time images do not display you will need to change the image paths in the CSS to be relative to your theme image directory.

Download

Download WordPress Display RSS Feed

Conclusion

This method of displaying your posts in another blog is fairly limited as there is only so much information contained in a RSS feed. If you have any questions or maybe have a better solution feel free to comment.

Sources

Use SimplePie to grab first image from a RSS feed

Topics:

173 replies on “Display WordPress Posts on another WordPress Blog”

  • Hi, thanks for this tutorial. It’s exactly what I need. I’m having a small issue though where one of the sites I’m trying to pull the feed from doesn’t seem to work. I get “No Items” instead of the feed. There are 3 posts on the site I’m trying to pull from and I can’t find any reason it wouldn’t. If I change the URL to other sites it works perfectly though. Any ideas why it won’t display posts from that site?

  • Hi. Is there a way to attribute different styles to different categories? I looked at the feed, and it seems like it is getting categories, but I couldn’t figure out how to edit the code so it’d be something like, “if category xxx: ; else “.
    Thank you.

  • Can some one tell me how to get excerpt and the name of the category of the post It will really help Thanks

  • Hello ! Thanks for your very useful tutorial. I posted a comment yesterday, seems not to have worked.
    I was asking how could I bring the img url to
    Here’s my code : http://pastebin.com/zTUG94q5

    Could you help?
    Thanks a lot.

    • This can be done. You forgot to omit the double quotes and periods in your code. See http://pastebin.com/2EB6NeAa

      • Hi Tracy! Thanks for your reply 🙂 Unfortunately, this doesn’t work. I tried so many combos, nothing works. Maybe this should and because of my WP and theme install something fails there. But not including double quotes and periods leads to the footer of my page (where the “feed rss” div is) not loading completely!

      • It could be a theme issue. Do you have support for thumbnails in your functions.php and do the posts have images? Have you looked to see if there is a typo anywhere else?

      • Hello ! Yes the add_theme_support line with post thumbnails is in my functions.php. The posts have images, my external website also is okay with all the features. I don’t think there’s an error in the code. I’ll try again today and hope that I’ll see the end of it!

      • Any luck? You could test it out with another RSS Feed and test it with the default WP theme.

  • Very Nice Thank you

  • Hello ! Very usefull tutorial thanks! How could I echo the image url instead of echo the image itself? I’ve tried many combos, nothing worked yet unfortunately.

  • How to display posts for a specific author?

  • how to display post using author?

  • Great tutorial. Thanks

  • how I resize the rss images?

  • Hi,

    Now i got it working with blogs authors name: get_author(); echo $author->get_name(); ?>

    But is it possible get a blogs name from multisite front page feed?

    I try many codes, but it always show front page site title not blogs title.

    Thanks,
    Jaybee

  • Hi,

    This is awesome tutorial!

    One question:

    Is it possible show to feed author/blog name?

    I have feed from multisite front page, where is 20 sites and everythings work fine without author.

    If i try show feed author/blog name, then it show only blog author where i show that feed.

    This is my code:

    Sorry my bad english 🙁

  • Hello, Great Work!

    Umm… can i ask something?

    How to do something like:

    I mean displayed multiple feed from another blog, but it’s in different section, not like Simon Podcaster Comment, please help me.

    Thank you and great work~

    • Haven’t tried that yet. I’ll look into it and let you know.

      • Wow, thanks for reply!
        I’ll wait for it, bcus i need it for my site 🙂
        Just like Simon Podcaster Comment but in this case, i need to display multi site, but in differrent section, i try add those snippet code in 2 row, but it makes my site blank..

        how to solve it?
        I’m newbie here, Thank you

      • The original article was just for a single feed then added an option to combine the feeds in an array to display multiple feeds together.

        Your situation was slightly different so I have added the functions to a class to display multiple separate feeds which I have got working. Checkout http://pastebin.com/8pkMUd6S

        The instructions are written at the top of the class but make sure you remove any of the previous code except for post thumbnail code in functions.php. The CSS is the same so you can style it how you want.

  • Are you fetching feeds from a single blog or multiple blogs? Can you provide any URLS?

  • the images are coming to my localhost server but not to my live demo server. i am trying to get blogs from trackschoolbus.com to beta.jointviews.com (prot-pwd- ‘ joint123 ‘) the images came from the feed given in the example but not from mine

  • Is there a way to add multiple RSS feeds to line 2? They’re all on the same WordPress Multisite network.

  • Thanks so much for this. I’m running into a problem with an apparent syntax error on line 11 and I can’t seem to get around it. It won’t pull my feed. It keeps giving me a code 400. What can I do?

  • When i use other urls for the rss/feed it works fine but when i tried to use a subdomain.domain.com it doens’t work… why???

    • Not sure. Haven’t come across that problem before. Maybe it could be something to do with a re-direct. Are there any errors coming up, either on screen or in your logs?

  • Thanks a lot Tracy. This is what i was searching for. m/ Thanks once again 🙂

  • I love this tutorial – great work!! But I’m running into one odd thing. The material I’m seeing is not what I’m seeing in the rss feed. For instance, in one case, I deleted a post, in another, I corrected a misspelling, and in a third, I changed a photograph. When I look at the RSS feed anywhere else, I see the updated content, but on the site where I’ve used this tutorial, I’m still seeing the old material. Shouldn’t it just show whatever is on the RSS feed? Is there a way to force a refresh?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.