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”

  • Dear Tracy Ridge,
    Thanks for great post !

    I have 2 wordpress blog sites: Site A and Site B, both of them were put on the same hosting. Database of Site A has prefix is sitea_ and Database of Site B is siteb_. Site B has 4 categories which is B1,B2,B3,B4. How do i do to show 5 newest posts in B1 of site B on site A ? Thanks so much again !

  • Hey, thanks so much for this. I just used it with the adapt responsive theme to pull posts from my other blog. I don’t know what I would’ve done without this post. Thanks, again!

  • Good information , thanks for sharing

  • Hey Tracy,

    I have an error : No items found. Whats might be the problem? I made everything same.as you.

  • This is awesome! Is there any way to use the featured image instead?

  • hello,

    I’ve searched all the internet lol and nothing so far, could someone explain in the css how I can display the rss feed as shown in the image?

    Everyting is working but I can get the image to align to the text

    I’ve tried with tables and nothing

    Help:::!

    thanks

    • Have you added the CSS from the post? I suggest you install the Firebug extension or use your browser debugger to play around with the CSS then make the changes permanently when you are happy. A tip for the CSS is to make small changes and ensure that you do a full browser refresh as your browser may use a cached stylesheet. Can you provide me with a URL and I’ll take a look?

  • hi again, this is a question that may actually solve my previous error issue, where exactly is the code supposed to go. I have a index.php and a home.php for my theme. the home.php is where the blog posts appear on the homepage. index.php shows the structure (header, maincontent, footer, etc)

    Also is it possible for when you click on the title that comes in on the rss feed to open the blog post in a new window?

  • Well at first I thought it was working, but I get a error message whenever I try to create a new post or trash one.

    Warning: Cannot modify header information – headers already sent by (output started at /homepages/44/d393976557/htdocs/wp-content/themes/DelicateNews/functions.php:17) in /homepages/44/d393976557/htdocs/wp-includes/pluggable.php on line 881

    any suggestions?

    • That particular error is your functions.php. You must have some whitespace either before or after (or both) the php opening and closing tags. Delete the spaces and the error should go away.

  • Hello, I must say your code is awesome! I have been looking for something like this for over year. I decided to do a search again today and your post came up! 🙂

    I would like to know how would I include a read more link. On my current website, we have read more links at the bottom of each description or excerpt. Could you tell me how to add it in this code. I believe it’s called pagination.

  • The code works perfect for me but I want the image to be of a custom size. I have custom sizes set up for my thumbnails but when I change it in the code I always get the big image in the RSS:
    [code]
    if ( function_exists( ‘add_image_size’ ) ) add_theme_support( ‘post-thumbnails’ );
    if ( function_exists( ‘add_image_size’ ) ) {
    add_image_size( ‘home1-thumb’, 630, 320, true );
    }
    function cwc_rss_post_thumbnail($content) {
    global $post;
    if(has_post_thumbnail($post->ID)) {
    $content = ” . get_the_post_thumbnail($post->ID,’home1-thumb’) .
    ” . get_the_excerpt();
    }
    return $content;
    }add_filter(‘the_excerpt_rss’, ‘cwc_rss_post_thumbnail’);
    add_filter(‘the_content_feed’, ‘cwc_rss_post_thumbnail’); [/code]

  • Hey, thank you for this. I was looking for something like this for long time. I have a one question if it is not problem and if i am not bothering you. Is there a way i can offset first item like in regular loop. I mean is there way to filter out first post from the rss feed? Thank you for your time

    • Hey, I allredy figured it out. Sorry for bothering

      • No bother at all. Glad you figured it out. Feel free to share your code with others, just remember to wrap them in pre tags.

      • Ok, so if anyone had this problem here is the solution, if you want to filter one or few latest posts you can do that in line 8 from zero to number of posts you want to exclude, for example if you want your lood exclude 4 latest posts from feed, your line 8 should look like this

         $rss_items = $rss->get_items(4, $maxitems);
        
  • Separate question. (I am going to have to buy you a beer.) Do you see new posts immediately after publishing? It seems to taks 2 hours for new posts to show.

  • Anyone else have an issue where get_description() with get_content() only pull an excerpt? I’m having a hard time pulling full post.

  • hi : Tracy Ridge

    Can I get code to bring from some Categories

    thanks

  • “Please note that linking to an external blog which is not your own may result in no images being displayed.”

    Does something have to be enabled on the external blog? Images are not getting pulled.

  • This code has helped me out immensely, thank you.

    How can I display the entire blog post? First I tried changing the shorten value to 1000, but it seems to be pulling the excerpt. Screen shot: http://i.imgur.com/y95YF.png

    Next I tried removing the shorten function and echoed get_description() – but this threw back a php error.

    Any advice?

    Thanks, again.

    Steve

  • hi : Tracy Ridge

    Thanks it works
    Great work

    Thank you

      • hi :

        How to include in Widgets?

        thanks

      • Unless you want to hard-code it into your theme I suggest you try this plugin

      • i try

        thanks

      • yes i add images to my blog posts I used PHP Code widget Plugin to Insert code and i test in two themes
        same problem

      • Strange, I got it working first time. Try testing it on the TwentyEleven theme? If that fails then copy the code from the post into the widget and try again. If that works switch back to your theme and test it again. If that fails then it is a theme problem.

      • i Try testing it on the TwentyEleven theme same problem

      • Did you try re-entering the code again into the widget? How did you copy the code to the widget? Have you used any other RSS plugin?

      • I use your feed code to testing

        look what i do

      • I think it makes sense now. U are trying to add 2 widgets with the code in? If I am right you need to strip the functions from one of the widget as they are declared twice, hence the error. Remove the following code from 1 of the widgets and try again.

        <?php function get_first_image_url($html)
             {
                if (preg_match('/<img.+?src="(.+?)"/', $html, $matches)) {
                    return $matches[1];
                }
             }
        ?>
         <?php
        function shorten($string, $length)
        {
            $suffix = '…';
            $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;
        }
        ?>
        
      • hi : Tracy Ridge

        I Remove the following code from 1 of the widgets and try again. and working now .
        can i used this code now
        Is there any other tips.

        thank you

    • hi : Tracy Ridge

      I used PHP Code widget it,s good working but when i add Second file php i see this problem

      what i can do?

      thanks

  • hi : Tracy Ridge

    thank i like this code but photo is emty how can show it?

    thanks

  • Wow, great article, I really appreciate your thought process and having it explained properly, thank you!

  • Hey man!

    Thanks for this but do you know how to put the ‘post author’ in there too??

    Thanks,
    Luke

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.