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

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

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

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.

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 WP2WP

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

Add Post Thumbnails to RSS feed
Use SimplePie to grab first image from a RSS feed

Related Posts

  • http://worldoweb.co.uk Ridgey

    Here is an example to filter posts from different blogs.
    http://pastebin.com/ieR9eUhu

  • Leo

    Nice code. But could you please tell me how to grab posts under specific categories?

  • nem

    Please, can you tell me, how can I fix this code so that we can show posts in a row, not in the list?

    • http://worldoweb.co.uk Ridgey

      You want them side by side or each on one row?

  • nem

    Can you tell me, do you post change ourselves, every time you add another blog to the new post. Since they are on my blog always appear the same postl, where I used this code

    • http://worldoweb.co.uk Ridgey

      Can you provide me with a URL so that I can take a look?

  • http://www.rockntech.com.br/ Simon Podcaster

    Hi, i have to say, this was FANTASTIC!!! But I have a doubt:

    I need to show the content from other 5 feeds in the same line. Is there a way to display the posts from these blogs, each item with their respective thumbnail, showing the newest firstly? I’ve tried to figure out, but no success. Help?

    • http://worldoweb.co.uk Ridgey

      I’ll test it out and let you know.

      • http://www.rockntech.com.br/ Simon Podcaster

        Ok! Thank you! ^^

      • http://worldoweb.co.uk Ridgey
      • http://www.rockntech.com.br/ Simon Podcaster

        Yup! I read, but first of all we need to put two or more feeds together, and then I think they will be showed sorted by date automaticaly, and if won’t, I’ll figure out the way to fix this. :

      • http://worldoweb.co.uk Ridgey

        Sorry about the delay. You can add your websites to an array like so.

        
        $rsslist = array( 'http://www.mywebsite.co.uk/feed',                          'http://www.myotherwebsite.co.uk/news/feed'
          );
        		
        $rss = fetch_feed($rsslist);
        

        So far I haven’t got the shorten function to work but will test later.

      • http://www.rockntech.com.br/ Simon Podcaster

        IT WORKS!!! And the best of all, it’s sorted by date automatically!

        That’s amazing, YOU ARE AMAZING!!!
        I’m deeply grateful for your help!!!

        Cheers! o/

      • http://worldoweb.co.uk Ridgey

        Your welcome. Glad it worked for you.

  • Neosix

    and if somebody wants to know how to display feed from specific post type just add this in the url: …/feed/?post_type=your_post_type

  • Neosix

    Great, just what I needed, thank you for sharing. Nice blog :)

  • nurul

    Hi, nice post.

    I would like to ask how to get posts from specific category?

  • Rabin Gurung

    i would like to add leave comment wordpress blog but i have no idea

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Hi Rabin
      Do you mean a link to direct you to comment on the post?

      Add the following to the loop. Had to use pastebin as code not displaying properly in comment section.
      Add comment link to blog

  • Qazi Atif

    hi preety much trick
    i want to show my all post into other domain (wordpress blog). As you can say i want to provide and script to my reader to use my all news into their blog. i am confused how to do this thing.
    can u be please help me out in this ……….
    you can check my website http://jazaa.org

    i will realy thinkfull tou you………..

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Hi Qazi

      I removed your email link to prevent you from spammers. What is the URL of the blog you want to display your posts in?

  • Ashok Thakur

    This is awesome! Thanks

  • Dubai web design, development

    This example is fine, but little bit confused. I want to show posts on my website from my blog, not from rss. Please let me know how can i do that?

  • Jerry

    You should consider doing a version that shows images. The function you included does not work.

    I found a function almost identical that worked when I plugged it in, but I can’t find it how. This is ridiculous how convoluted and wasteful you WordPress people are. Spending 24 hours to move a stupid article to another page is pathetic and does not bode well for the survival of the human race in general.

    • http://www.worldoweb.co.uk/ Tracy Ridge

      This post displays your post thumbnails, as stated. Unfortunately there is only so much you can do with a RSS feed otherwise I would have added more. There are probably many other ways. You could always ask Google or post a question on one of the various forums then someone might be able to help although it does require some patience and probably some good manners! I apologise in advance if I have offended you but sometimes you just need to perservere.

  • Jerry

    Cool. Too bad you didn’t mention where to put that ‘snippet’ of code in the theme template. I put it on top, at the bottom, in the middle, integrated, replaced, no matter what. Nothing happens at all. Cheers.

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Put it where you want to display your latest posts.

  • x

    thanks looking for this …

  • Ricky1990

    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 !

    • http://www.worldoweb.co.uk/ Tracy Ridge

      I will see if I can replicate the scenario and I will get back to you.

      • http://www.worldoweb.co.uk/ Tracy Ridge

        I have found a way but unfortunately it is very buggy. The next version of WordPress will upgrade to the latest version of SimplePie which should make things much better then I’ll let you know more.

  • Eliot Fowler

    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!

  • Steven

    Good information , thanks for sharing

  • Anna

    Hey Tracy,

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

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Hi Anna. Have you published anything on your blog? What is your URL and what are you linking to?

  • Robert

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

  • joel cunha

    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

    • http://www.worldoweb.co.uk/ Tracy Ridge

      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?

  • Angela Vaughn

    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?

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Generally the code can go into any template. It depends on where you want it positioned. Just be careful not to place it in the loop! You can also display it using a widget in the sidebar with the following plugin: http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/

      You then add the code directly to the widget.

      To open the page in a new window or tab add the following to the a tags. Example below.

      Ignore the nofollow just add target=”blank”

  • Angela Vaughn

    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?

    • http://www.worldoweb.co.uk/ Tracy Ridge

      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.

  • Angela Vaughn

    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. http://salvationarmychicago.net/redshieldcenter/

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Simply add the following underneath the description (excerpt).

      Read More

      Then you can style the read-more class in your css file. An example would be.

      .read-more
      {
          background-color:#3C3030;
          padding:7px;
      }
      
  • Maggie

    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]

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Are you getting any error messages? Have you tried adding a width to the img tag?

  • Miro

    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

    • Miro

      Hey, I allredy figured it out. Sorry for bothering

      • http://www.worldoweb.co.uk/ Tracy Ridge

        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.

      • Miro

        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);
        
  • Steve

    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.

    • http://www.worldoweb.co.uk/ Tracy Ridge

      You are lucky I like Beer. ;-) The cache is set at 3600 seconds. To change it add the following underneath the feed include:

      include_once (ABSPATH . WPINC . '/feed.php');//place under here
      add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', 'return 120;' ) );
      
  • Steve

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

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Have you enabled; for each article in a feed, show full text in the WordPress Reading Settings.

      • Steve

        I have, unfortunately. Do you think it may have something to do with the WordPress installation being on a subdomain?

      • http://www.worldoweb.co.uk/ Tracy Ridge

        Is the full content in the XML feed? Have a look at the source file

      • Steve

        I have checked it with feed burner and it shows the excerpt with read more link. I am starting to wonder if it is because of the way the posts are display on the blog…

      • http://www.worldoweb.co.uk/ Tracy Ridge

        It could be a theme related problem? Which theme are you using?

  • http://www.egyptianweb.org/ ayman

    hi : Tracy Ridge

    Can I get code to bring from some Categories

    thanks

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Get posts from categories or display the category?

      • http://www.egyptianweb.org/ ayman

        display the category

      • http://www.worldoweb.co.uk/ Tracy Ridge

        @ayman Place < ?php the_category();?> within the loop to display your categories

  • Steve

    “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.

    • http://www.worldoweb.co.uk/ Tracy Ridge

      If it is a WP blog and its your own then just make sure you use the post-thumbnails feature and add the snippet in functions.php. If you wanted to link to another blog, let’s say http://www.smashingmagazine.com then you could only pull an image if they have used one. There is no guarantee.

      • Steve

        The below snippet was already in functions.php – Maybe the image not being pulled because it is in the post body? It is outputting
        add_theme_support( ‘post-thumbnails’ );

      • http://www.worldoweb.co.uk/ Tracy Ridge

        Do you have a URL?

      • http://www.worldoweb.co.uk/ Tracy Ridge

        Try changing

         
                    < ?php echo ''; ?>
                 

        To

         
                    < ?php echo ''; ?>
                 

        To try and fetch the first available image within the content

    • Josephine

      Hi, making use of the built-in lirairbes within WordPress (mainly Transients and HTTP API) would make this code much simpler and more portable.Here’s a quick one shot of how it could work: https://gist.github.com/900356

  • Steve

    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

    • Steve

      Oh, simply replace get_description with get_content().

      :) Thanks, again.

      • http://www.worldoweb.co.uk/ Tracy Ridge

        Glad you figured it out. Sometimes it just takes a little perseverance to get the desired result. It’s a great way to learn.

  • http://www.egyptianweb.org ayman

    hi : Tracy Ridge

    Thanks it works
    Great work

    Thank you

    • http://www.worldoweb.co.uk/ Tracy Ridge

      No problem

      • http://www.egyptianweb.org/ ayman

        hi :

        How to include in Widgets?

        thanks

      • http://www.worldoweb.co.uk/ Tracy Ridge

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

      • http://www.egyptianweb.org ayman

        i try

        thanks

      • http://www.egyptianweb.org/ ayman

        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

        see this pic

        http://www14.0zz0.com/2012/08/09/17/433392312.jpg

      • http://www.worldoweb.co.uk/ Tracy Ridge

        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.

      • http://www.egyptianweb.org ayman

        i Try testing it on the TwentyEleven theme same problem

        Look : http://www4.0zz0.com/2012/08/09/19/190522060.jpg

      • http://www.worldoweb.co.uk/ Tracy Ridge

        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?

      • http://www.egyptianweb.org ayman

        I use your feed code to testing

        look what i do

        http://www3.0zz0.com/2012/08/09/21/237613412.jpg

      • http://www.worldoweb.co.uk/ Tracy Ridge

        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('/	
         < ?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;
        }
        ?>
        
      • http://www.egyptianweb.org ayman

        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

    • http://www.egyptianweb.org ayman

      hi : Tracy Ridge

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

      http://www8.0zz0.com/2012/08/09/00/761402180.jpg

      what i can do?

      thanks

      • http://www.worldoweb.co.uk/ Tracy Ridge

        I will test it out myself and let you know the outcome. Did you add images to your blog posts?

      • http://www.worldoweb.co.uk/ Tracy Ridge

        I have tested it myself and it works perfectly. Have you removed all the code from the Display your latest posts on a static page from your templates. What theme are you using?

  • http://www.egyptianweb.org ayman

    hi : Tracy Ridge

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

    thanks

  • http://atechprofessionals.com/ Php classes

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

  • Luke Etheridge

    Hey man!

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

    Thanks,
    Luke

    • http://www.worldoweb.co.uk/ Tracy Ridge

      Hi Luke. Try adding this:

      < ?php $author = $item->get_author();?> 
      < ?php echo $author->get_name(); ?>
      
      • Luke Etheridge

        Hi Tracy, thanks for this – worked perfectly!

      • http://www.worldoweb.co.uk/ Tracy Ridge

        Your welcome ;-)