The MU forums have moved to WordPress.org

Recent Posts (24 posts)

  1. GMeagle86
    Member
    Posted 16 years ago #

    I have searched and tried about 10 different versions of a feature that would allow me to display the latest 10 posts from all of my sitewide blogs in one of my columns.

    A few requirements:
    1. Must be post title only.
    2. Must be code- not a widget
    3. Must have a link to the post

    The closest code I found was this:

    <h2>Last Updated</h2>
    <div class="comments">
    <ul>
    <?
    $blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE domain!='beblogger.com' AND last_updated!='0000-00-00 00:00:00' ORDER BY last_updated DESC LIMIT 10");
    foreach ($blogs as $blog) :
    $blog = "wp_".$blog."_options";
    $opt = $wpdb->get_col("SELECT option_value FROM $blog WHERE option_name='siteurl'");
    $opt2 = $wpdb->get_col("SELECT option_value FROM $blog WHERE option_name='blogname'");
    print "<li><a href='$opt[0]'title='$opt2[0]'>".ucfirst($opt2[0])."</a>";
    endforeach;
    ?>
    </ul>
    </div>

    The problem with this is that it didn't show the individual posts, only the blogs that the posts were made from.

    Thanks So much in advance!!

  2. heyguy
    Member
    Posted 16 years ago #

    I use the sitewide RSS feed plugin to generate the sitewide feed, and then the built-in fetch_rss() function to display the last x number of posts. I do the same thing for recent sitewide comments.

  3. dsader
    Member
    Posted 16 years ago #

    heyguy, ditto.

  4. ekusteve
    Member
    Posted 16 years ago #

    This plugin is another option:

    http://wpmudevorg.wordpress.com/project/Most-recent-posts---updated

    Instead of using it as a plugin, I used the code from the plugin and just put it directly in my home.php....I can't remember now why I did it that way, but it works quite well for posts.

    Steve

  5. GMeagle86
    Member
    Posted 16 years ago #

    I tried pasting the code directly into my sidebar.php and it didn't do anything... what is wrong?

    Thanks

    <?php
    /*
    Plugin Name: WordPress MU Recent Posts
    Plugin URI: http://atypicalhomeschool.net/wordpress-plugins/
    Description: Retrieves a list of the most recent posts in a WordPress MU installation. Based on (Andrea - fill this in)
    Author: Ron Rennick
    Author URI: http://atypicalhomeschool.net/
    */
    
    /*
    Version: 0.31
    Update Author: Sven Laqua
    Update Author URI: http://www.sl-works.de/
    */
    
    /*
    Parameter explanations
    $how_many: how many recent posts are being displayed
    $how_long: time frame to choose recent posts from (in days)
    $titleOnly: true (only title of post is displayed) OR false (title of post and name of blog are displayed)
    $begin_wrap: customise the start html code to adapt to different themes
    $end_wrap: customise the end html code to adapt to different themes
    
    Sample call: ah_recent_posts_mu(5,30,true, '<li>', '</li>'); >> 5 most recent entries over the past 30 days, displaying titles only
    */
    function ah_recent_posts_mu($how_many, $how_long, $titleOnly, $begin_wrap, $end_wrap) {
    	global $wpdb;
    	$counter = 0;
    	// get a list of blogs in order of most recent update
    	$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
    		last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
    		ORDER BY last_updated DESC");
    
    	if ($blogs) {
    		foreach ($blogs as $blog) {
    			// we need _posts and _options tables for this to work
    			$blogOptionsTable = "wp_".$blog."_options";
    		    	$blogPostsTable = "wp_".$blog."_posts";
    			$options = $wpdb->get_results("SELECT option_value FROM
    				$blogOptionsTable WHERE option_name IN ('siteurl','blogname')
    				ORDER BY option_name DESC");
    		       // we fetch the title and link for the latest post
    			$thispost = $wpdb->get_results("SELECT post_title, guid
    				FROM $blogPostsTable WHERE post_status = 'publish'
    				AND post_type = 'post' AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY)
    				ORDER BY id DESC LIMIT 0,1");
    			// if it is found put it to the output
    			if($thispost) {
    				if ($titleOnly == false) {
    					echo $begin_wrap.'<a href="'.$thispost[0]->guid
    					.'">'.$thispost[0]->post_title.'</a> <br/> by <a href="'
    					.$options[0]->option_value.'">'
    					.$options[1]->option_value.'</a>'.$end_wrap;
    					$counter++;
    					} else {
    						echo $begin_wrap.'<a href="'.$thispost[0]->guid
    						.'">'.$thispost[0]->post_title.'</a>'.$end_wrap;
    						$counter++;
    					}
    			}
    			// don't go over the limit
    			if($counter >= $how_many) {
    				break;
    			}
    		}
    	}
    }
    ?>
  6. GMeagle86
    Member
    Posted 16 years ago #

    Ok, I figured out one problem- a line of code needed to be added where I wanted the output to occur.

    <?php ah_recent_posts_mu(10,60,true, '

  7. ', '
  8. '); ?>

    However, it appears to only be outputting the most recent post per blog, and that's it. Any ideas?

  • andrea_r
    Moderator
    Posted 16 years ago #

    That's exactly what it's supposed to do.

  • heyguy
    Member
    Posted 16 years ago #

    Most recent posts plugin gives you the lastest post from each blog, the rss feed gives you the latest posts across the site no matter which blogs they came from.

  • andrea_r
    Moderator
    Posted 16 years ago #

    Unless they are marked as private.

  • GMeagle86
    Member
    Posted 16 years ago #

    Ok, I've tried to install the sitewide RSS-- What code do I use to put it in the sidebar?

    Thanks

  • myisha
    Member
    Posted 16 years ago #

    I installed the wmu sitewide plugin.how do you call the fetch_rss() function so that I can display the most recent post on the homepage?

  • akeel
    Member
    Posted 16 years ago #

  • myisha
    Member
    Posted 16 years ago #

    No replies..anyone?:(

  • dsader
    Member
    Posted 16 years ago #

    Here's one.

    <?php
     $rss = @fetch_rss('http://your.blog/wpmu-feed/'); ?>
    <ul>
    <?php for($i=0;$i<5;$i++) {
    $item=$rss->items[$i]; ?>
    <li><a href='<?php echo $item['link']; ?>'
    title='<?php echo $item['title']; ?>'>
    <?php echo $item['title']; ?>
    </a><p><?php echo $item['summary']; ?><p></li>
    <?php } ?>
    </ul>
  • dsader
    Member
    Posted 16 years ago #

    Here's another that I use it to parse feed from a punbb forum:

    function forums_rss() {
     require_once(ABSPATH . WPINC . '/rss.php');
    $url = html_entity_decode('http://your.rss/feed/');
    $rss = fetch_rss($url);
    $title = $rss->channel[title];
    $link = htmlentities($rss->channel[link]);
    $copy = $rss->channel[copyright];
    $author = $rss->channel[creator];
    $date = $rss->channel[pubdate];
    $desc = $rss->channel[description];
    $disp .= "\t<h2>$title</h2>\n";
    $num_items = 15;
    	if ( is_array( $rss->items ) ) {
    $rss->items = array_slice($rss->items, 0, $num_items);
    foreach($rss->items as $item) {
    	$title = $item[title];
    	$link = htmlentities($item[link]);
    	$desc = substr($item['description'],0,75);
    	$text = strip_tags($desc,'');
    	$author = $item[creator];
    	$disp .= "\t<a href='$link'>$title</a>\n";
    	if ($title != '') $disp .= "\t<p>$text...</p><hr />\n";
    }
    }
    echo $disp;
    }
  • myisha
    Member
    Posted 16 years ago #

    @dsader thank you.I'm going try it.I'm tell you if I could get it working.

  • GMeagle86
    Member
    Posted 16 years ago #

    I tried both pieces of code dsader posted, and neither worked- they didn't show up in the place where I had the code, however there wasn't any error message. All I changed was the site feed link. Any ideas?

    Thanks!

  • dsader
    Member
    Posted 16 years ago #

    GMeagle86,
    For the second, you'll need
    '<?php forums_rss(); ?>`
    to display the out put of the function. Sorry, forgot to make that easy.

    What's your feed? I'll help ya troubleshoot the first method again.

  • dsader
    Member
    Posted 16 years ago #

    Oh, I see what's missing from the first, sorry.
    require_once (ABSPATH . WPINC . '/rss.php');

  • GMeagle86
    Member
    Posted 16 years ago #

    dsader-
    Here is the code. It is put into rsidebar.php:

    <?php
     $rss = @fetch_rss('http://hoosieraccess.com/wpmu-feed/');
     require_once (ABSPATH . WPINC . '/rss.php');?>
    <ul>
    <?php for($i=0;$i<5;$i++) {
    $item=$rss->items[$i]; ?>
    <li><a href='<?php echo $item['link']; ?>'
    title='<?php echo $item['title']; ?>'>
    <?php echo $item['title']; ?>
    </a><p><?php echo $item['summary']; ?><p></li>
    <?php } ?>
    </ul>

    It still comes up with nothing. Thanks for your help.

  • dsader
    Member
    Posted 16 years ago #

    You've got the cart before the horse, as they say. fetch_rss is in rss.php, therefore
    this will work

    <?php
     require_once (ABSPATH . WPINC . '/rss.php');
     $rss = @fetch_rss('http://hoosieraccess.com/wpmu-feed/'); ?>

    while this will not

    <?php
     $rss = @fetch_rss('http://hoosieraccess.com/wpmu-feed/');
     require_once (ABSPATH . WPINC . '/rss.php');?>
  • GMeagle86
    Member
    Posted 16 years ago #

    Ok, it worked. FYI- I'm working on this:
    http://test.hoosieraccess.com/

    I was wondering if there was a way to just display the post titles, and not the summary. Then have the title be a link to the actual post...

    Thanks!

  • dsader
    Member
    Posted 16 years ago #

    Simple drop out the summary code <p><?php echo $item['summary']; ?><p>

    But something is very wrong with your feed, no titles are being sent??

  • wessa
    Member
    Posted 16 years ago #

    I am trying to use the fetch rss funtion for my site to show recent posts. Currently using feedlist (via feedburner). For some reason my feed won't show, but if I replace my feed with other wpmu users sitewide feed urls it works fine. There must be something wrong with my feed. Here is the code I am using...

    <?php
    require_once (ABSPATH . WPINC . '/rss.php');
     $rss = @fetch_rss('http://nourishedmagazine.com.au/wpmu-feed/'); ?>
    <ul>
    <?php for($i=0;$i<5;$i++) {
    $item=$rss->items[$i]; ?>
    <li><a href='<?php echo $item['link']; ?>'
    title='<?php echo $item['title']; ?>'>
    <?php echo $item['title']; ?>
    </a></li>
    <?php } ?>
    </ul>
  • About this Topic