The MU forums have moved to WordPress.org

Modified ah_recent_recent to show the author's gravatar (25 posts)

  1. d_kc
    Member
    Posted 15 years ago #

    I'm using Gravatars and I'm looking at the ah plugin: http://wiki.evernex.com/index.php?title=Wordpress_MU_sitewide_recent_posts_plugin

    I'm not sure where to start so that the plugin also grabs the userid, so it can also output the gravatar of the author of the recent post and if no gravatar is available to show an Identicon.

    Basically what i want is to integrate the built-in gravatar function into this plugin.

    Any pointers ?

  2. d_kc
    Member
    Posted 15 years ago #

    Well I've been reading up and tried to do it myself, but so far nothing seems to work.

    I'll keep trying. This might be plugin competition worthy, no ? :D

  3. d_kc
    Member
    Posted 15 years ago #

    This is some of the code from the plugin and i've added the code for gravatars using the user_login.

    Nothing shows up ..when i view source it shows " <?php echo get_avatar( admin , 64 ); ?>" in a pink color. What does that mean ?

    // if it is found put it to the output
    			if($thispost) {
    			// pull in the posts content from database
    			$desc = $thispost[0]->post_content;
    			// save the post date to a var
    			$date = strtotime($thispost[0]->post_date);
    			// strip out html characters to allow for truncating
    			$strippedDesc = strip_tags($desc);
    			// truncate post content to 120 words
    			$numwords = 120;
     			preg_match("/([\S]+\s*){0,$numwords}/", $strippedDesc, $regs);
    		    $shortDesc = trim($regs[0]);
    				// Displays the post title url truncted text and soon author
    				// Author currently returns the ID (number) I will have to compare this to display_name and return a string name...
    				echo '<h3>'
    				.$thispost[0]->post_title.'</h3>'
    				.$shortDesc.'
    				<?php echo get_avatar( '.$thispost[0]->user_login.' , 64 ); ?>
    				<a href="'.$thispost[0]->guid
    				. '"> ...Read this article.</a>
    ';
    				$counter++;
  4. dsader
    Member
    Posted 15 years ago #

    Pay closer attention to where you've opened and closed php tags. It's all mucked up. You have tags within tags and echo within echo. Can't do that.
    Example:
    . $shortDesc . get_avatar

  5. d_kc
    Member
    Posted 15 years ago #

    could you elaborate a bit more ?
    I understand about the wrapping tags... but with just get_avatar( '.$thispost[0]->user_login.' , 64 ); ..i cant get it to work.

  6. d_kc
    Member
    Posted 15 years ago #

    This is what the plugin looks like now. A gravatar shows up now ...but it's not grabbing the right avatar for the post author. Currently it's displaying the same identicon for different users.

    <?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)
    Version: 0.3
    Author: Ron Rennick
    Author URI: http://atypicalhomeschool.net/
    
    Use this by pasting the following wherever you want it to show:
    <?php ah_recent_posts_mu(10) ?>
    
    */
    
    function ah_recent_posts_mu($how_many = 10) {
    	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
    		public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 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_id, option_name DESC");
    		       // we fetch the title and link for the latest post
    		$thispost = $wpdb->get_results("SELECT post_title, guid, post_content, post_date, user_login
    											FROM $blogPostsTable, wp_users
    											WHERE wp_users.ID = $blogPostsTable.post_author
    											AND post_status = 'publish' AND post_type = 'post' AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 DAY)
    											ORDER BY $blogPostsTable.id DESC limit 0,3");
    			// if it is found put it to the output
    			if($thispost) {
    			// pull in the posts content from database
    			$desc = $thispost[0]->post_content;
    			// save the post date to a var
    			$date = strtotime($thispost[0]->post_date);
    
    			// strip out html characters to allow for truncating
    			$strippedDesc = strip_tags($desc);
    			$avatar = get_avatar( '.$thispost[0]->user_login.' , 64 );
    			// truncate post content to 12 words
    			$numwords = 12;
     			preg_match("/([\S]+\s*){0,$numwords}/", $strippedDesc, $regs);
    		    $shortDesc = trim($regs[0]);
    				// Displays the post title url truncted text and soon author
    				// Author currently returns the ID (number) I will have to compare this to display_name and return a string name...
    				echo '<li><h2><a href="'.$thispost[0]->guid
    				. '">'.$thispost[0]->post_title.'</a></h2>'
    				.$shortDesc.'
     				'.$avatar.'
    				<a href="'.$thispost[0]->guid
    				. '">[...]</a>
    				</li>';
    				$counter++;
    			}
    			// don't go over the limit
    			if($counter >= $how_many) {
    				break;
    			}
    		}
    	}
    }
    ?>
  7. dsader
    Member
    Posted 15 years ago #

    $avatar = get_avatar( '.$thispost[0]->user_login.' , 64 );
    that'll still just echo code(therefore no ID_email and the defualt image shows for everyone). Instead

    $avatar = get_avatar( $thispost[0]->user_login , 64 );

  8. dsader
    Member
    Posted 15 years ago #

    an echo html to browser line looks like this
    echo '<html>';

    echo a php

    echo $var;

    echo a php and html in same echo line

    echo '<html>' . $var;
    or
    echo $var . '<html>';

  9. d_kc
    Member
    Posted 15 years ago #

    Thanks for the tips. I learnt something today. :)

    It's still not showing the gravatar, but it now shows a different identicon for each user/post author.

    So what I need is something that outputs the email address and not the username. Any ideas ?

    Here is the plugin right now.

    <?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)
    Version: 0.3
    Author: Ron Rennick
    Author URI: http://atypicalhomeschool.net/
    
    Use this by pasting the following wherever you want it to show:
    <?php ah_recent_posts_mu(10) ?>
    
    */
    
    function ah_recent_posts_mu($how_many = 10) {
    	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
    		public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 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_id, option_name DESC");
    		       // we fetch the title and link for the latest post
    		$thispost = $wpdb->get_results("SELECT post_title, guid, post_content, post_date, user_login
    											FROM $blogPostsTable, wp_users
    											WHERE wp_users.ID = $blogPostsTable.post_author
    											AND post_status = 'publish' AND post_type = 'post' AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 DAY)
    											ORDER BY $blogPostsTable.id DESC limit 0,3");
    			// if it is found put it to the output
    			if($thispost) {
    			// pull in the posts content from database
    			$desc = $thispost[0]->post_content;
    			// save the post date to a var
    			$date = strtotime($thispost[0]->post_date);
    
    			// strip out html characters to allow for truncating
    			$strippedDesc = strip_tags($desc);
    			$avatar = get_avatar( $thispost[0]->user_login , 64 );
    			// truncate post content to 12 words
    			$numwords = 12;
     			preg_match("/([\S]+\s*){0,$numwords}/", $strippedDesc, $regs);
    		    $shortDesc = trim($regs[0]);
    				// Displays the post title url truncted text and soon author
    				// Author currently returns the ID (number) I will have to compare this to display_name and return a string name...
    				echo '<li><h2><a href="'.$thispost[0]->guid.'">
    				'.$thispost[0]->post_title.'</a></h2>
    				'.$shortDesc.'
     				'.$avatar.'
    				<a href="'.$thispost[0]->guid
    				. '">[...]</a>
    '.$thispost[0]->user_login.'
    				</li>';
    				$counter++;
    			}
    			// don't go over the limit
    			if($counter >= $how_many) {
    				break;
    			}
    		}
    	}
    }
    ?>
  10. d_kc
    Member
    Posted 15 years ago #

    Figured it out.

    final code:
    save as ah-recent-posts-mu.php

    Use

    <ul>
    <?php ah_recent_posts_mu(10) ?>
    </ul>

    to call in your home.php or wherever and set positioning with CSS. This will output the recent posts that are not private, spam, etc. It will also display 12 words from the content and gravatar size is to 64...so change that how you want. Hope this helps some future noob like me :)

    <?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)
    Version: 0.3
    Author: Ron Rennick
    Author URI: http://atypicalhomeschool.net/
    
    Use this by pasting the following wherever you want it to show:
    <?php ah_recent_posts_mu(10) ?>
    
    */
    
    function ah_recent_posts_mu($how_many = 10) {
    	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
    		public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 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_id, option_name DESC");
    		       // we fetch the title and link for the latest post
    		$thispost = $wpdb->get_results("SELECT post_title, guid, post_content, post_date, user_email, user_login
    											FROM $blogPostsTable, wp_users
    											WHERE wp_users.ID = $blogPostsTable.post_author
    											AND post_status = 'publish' AND post_type = 'post' AND post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 5 DAY)
    											ORDER BY $blogPostsTable.id DESC limit 0,3");
    			// if it is found put it to the output
    			if($thispost) {
    			// pull in the posts content from database
    			$desc = $thispost[0]->post_content;
    			// save the post date to a var
    			$date = strtotime($thispost[0]->post_date);
    
    			// strip out html characters to allow for truncating
    			$strippedDesc = strip_tags($desc);
    			$avatar = get_avatar( $thispost[0]->user_email , 64 );
    			// truncate post content to 12 words
    			$numwords = 12;
     			preg_match("/([\S]+\s*){0,$numwords}/", $strippedDesc, $regs);
    		    $shortDesc = trim($regs[0]);
    				// Displays the post title url truncted text and soon author
    				// Author currently returns the ID (number) I will have to compare this to display_name and return a string name...
    				echo '<li><h2><a href="'.$thispost[0]->guid.'">
    				'.$thispost[0]->post_title.'</a></h2>
    				'.$shortDesc.'
     				'.$avatar.'
    				<a href="'.$thispost[0]->guid
    				. '">[...]</a>
    '.$thispost[0]->user_login.'
    				</li>';
    				$counter++;
    			}
    			// don't go over the limit
    			if($counter >= $how_many) {
    				break;
    			}
    		}
    	}
    }
    ?>
  11. andrea_r
    Moderator
    Posted 15 years ago #

    Pleeeease use pastebin for large chunks of code. :)

    And glad you're having fun with our initial plugin. :)

  12. d_kc
    Member
    Posted 15 years ago #

    Thanks andrea.

    Having a prob with the links though. Its guid and thats incorrect for some posts.

    This
    $thispermalink = get_blog_permalink($blog, $thispost[0]->ID);
    and this
    <a href="'.$thispermalink.'">
    dont seem to be working. The link created is mainblog.com/2008/08/post-name instead of sublog.mainblog.com/2008/08/post-name

  13. andrea_r
    Moderator
    Posted 15 years ago #

    I really need to release the updated version. *sigh*

  14. d_kc
    Member
    Posted 15 years ago #

    Take your time :D

  15. indojepang
    Member
    Posted 15 years ago #

    2 andrea: lol :P yes, we're waiting!

  16. andrea_r
    Moderator
    Posted 15 years ago #

    Hmmm... well, the lines you're referencing don't seem to be ones in our plugin which was modified at that link given above, might wanna contact that guy. looks like a change he made.

    I should also note that our updated version (the Ron listed in the credits at the top of the plugin is my husband btw) it doesn't pull the avatars.

  17. d_kc
    Member
    Posted 15 years ago #

    Yeah well when you re-release, i'll just rework the avatars in now that I got a handle on it.

    as for the permalinks, through tons of searching, i found that guy's modified version but it doesn't work. Also found other techniques but all of them seem to just output either mainblog.com or mainblog.com/2008/08 instead of the right permalink to the blog post.

    Right now I'm looking at some other plugins to see how they output permalinks to sublog posts.

  18. andrea_r
    Moderator
    Posted 15 years ago #

    Newer bit:

    // 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 1 DAY)
    ORDER BY id DESC LIMIT 0,1");

    FWIW, I use this on a LOT of sites.

  19. d_kc
    Member
    Posted 15 years ago #

    then how to output link ?

    What about using "switch to blog" and then "restore blog" then the regular get_permalink functions in between? I tried this but my output is coming out blank.

  20. andrea_r
    Moderator
    Posted 15 years ago #

    What's your email?

  21. d_kc
    Member
    Posted 15 years ago #

    err... you can use admin -at- caribvine -dot- com

  22. Gaboinked
    Member
    Posted 15 years ago #

    Please tell us how the story ended! Email is the death to forum solutions. :) Did the hero kill the big broken bug? LOL In all seriousness, was there a fix for the link correction?

  23. Ovidiu
    Member
    Posted 15 years ago #

    is there somewhere a working version we could also download?

  24. taktemp
    Member
    Posted 15 years ago #

    hi.
    I use this cod to show lasts posts.
    bu t i have an answere.
    how to i show comments number and outher real name?
    (in this cod shows user login name).
    ??

  25. indojepang
    Member
    Posted 15 years ago #

    check this out!

About this Topic