Hire us to get Web Development smartly done... !!!

Get Recent Posts From Various Blogs On WordPress MU Site

If you wish to show recent posts in the sidebar or on the front page of your WordPress MU site, for example as I have put on Blogern, you can use the below custom function which accepts the blog_id and the number of posts links to display as input parameters:
Wordpress MU Code Snippet

Function to get recent posts from specific blog:

Put the below function in your active theme’s functions.php

function get_recent_blogposts_wpmu($blog_id,$show){
	wp_reset_query();
	switch_to_blog($blog_id);
	global $post;?>
	<?php $my_query = new WP_Query('showposts='.$show.'&order=DSC');
	while ($my_query->have_posts()) : $my_query->the_post();
	    $do_not_duplicate = $post->ID; ?>
	    <li class="post_link"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a> </li>
        <div style="float:left;margin:5px 0 0 10px;">
        <script type="text/javascript">
        tweetmeme_url = '<?php the_permalink(); ?>';
        tweetmeme_source = 'blogern';
        tweetmeme_service = 'bit.ly';
        tweetmeme_style = 'compact';
        </script>
        <script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"> </script>
        </div>
        <div style="clear:left;"></div>
	<?php endwhile; ?>
	<?php restore_current_blog();
}

How to use this function/place a call this function

In your template file like page.php or index.php (wherever you need) you can use the below template tag now:

get_recent_blogposts_wpmu('4','8');

This will get 8 posts from blog_id ’4′. The above function will show the tweetmeme button, the permalinks and title  for each of the eight recent  posts on that particular blog on WPMU site.

You can extend this function

You can add any WordPress template tag that can be added in normal WordPress Loop in this custom loop created under this function. For example, along with the permalink with title, you want the excerpt for the pulled posts, you can use the_excerpt template tag. You can modify the looks by using CSS classes. This is just a basic function for WordPress MU, you can use it in your own innumerable ways. 

No Trackbacks

You can leave a trackback using this URL: http://www.staenzwebsolutions.com/get-recent-posts-from-various-blogs-on-wordpress-mu-site/324/trackback/

8 Comments

  1. annie

    That’s really cool, thanks for posting!
    Is there a way to select a specific category from the blog where you’re getting the posts from?

    Posted April 24, 2010 at 6:45 am | Permalink | Reply
  2. Thats cool but unfriendly for non technical users .There is plugin available for it.

    Thanks

    Posted May 14, 2010 at 3:42 pm | Permalink | Reply
    • Nishant

      Where can I get the plugin

      Posted June 24, 2010 at 2:59 pm | Permalink | Reply
      • admin

        there is no plugin, this is the code written by me for my WPMU site. You can include this code and it is too simple to get the recent posts.

        Posted June 26, 2010 at 12:58 pm | Permalink | Reply
  3. Grant

    Hey, this is very cool! Can this be broken down by category, tag and/or author of the blog as well?

    Thanks

    Posted July 13, 2010 at 2:20 am | Permalink | Reply
  4. Grant

    Hey,

    So I got it to work with the category, but I can’t get a thumb to show up. Heres the code, any suggestions?

    function get_recent_blogposts_featured_nfl($blog_id,$show){
    wp_reset_query();
    switch_to_blog($blog_id);
    global $post;?>
    have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    // $blog_temp = echo bloginfo(‘template_directory’);
    // $pic_loc = echo get_post_meta($post->ID, ‘thumb’, $single = true);
    // $resized_image = $blog_temp.”/tools/timthumb.php?src=.”$pic_loc.”&h=150&w=350&zc=1″; ?>
    <img src="/tools/timthumb.php?src=ID, “thumb”, $single = true); ?>&h=150&w=350&zc=1″ /><a href="” rel=”bookmark”>

    <?php restore_current_blog();
    }

    Thanks

    Posted July 16, 2010 at 7:21 am | Permalink | Reply
    • Tejaswini Deshpande

      Grant, you are using “echo” in a wrong manner. It is used when you want to display something on the web browser. But in your case you need the data in a variable, say $blog_temp. So you should use
      $blog_temp = get_bloginfo(“template_url”);
      $pic_loc = get_post_meta($post->ID, ‘thumb’, $single = true);
      $resized_image = $blog_temp.”/tools/timthumb.php?src=”.$pic_loc.”&h=150&w=350&zc=1″;

      And then u can display the image as you tried to do in your function with “src” equal to $resized_image. No need to use the timthumb reference again.

      I am not writing php code in comment, as it does not get displayed properly. Please post me if you have any query thru the contact form, so that I could post you back the code, thru email

      Posted July 17, 2010 at 12:14 am | Permalink | Reply
  5. Sandeep

    Hi ,
    Is there any way that we could get the latest posts outside the word press mu site .

    for instance I have one main website (written in php ) and in that i have a sub blog site that is entirely created using word press mu , I need to get the titles of the latest 10 blogs from the blog site and display them in the home page of the main site . , any help is greatly appreciated

    and by the way you did a great job with the function tejaswini

    SaNdEeP

    Posted July 19, 2010 at 8:26 pm | Permalink | Reply

Post a Comment

Your email is never shared. Required fields are marked *

*
*