*** How to pull in latest post featured image as Blog Avatar in Site Direcory ***

A little code snippet for those (especially those who rum a multisite) - Add this to your child theme functions.php file. What it does is use the latest post from your blogs as the blog avatar in the blogs/site directory, rather than use the blog creators personal avatar… (see image below)

add_filter('bp_get_blog_avatar', 'bpmmh_handle_blog_avatar', 3, 9 );

function bpmmh_handle_blog_avatar( $avatar, $blog_id, $admin_avatar_args = false ) {
	global $blogs_template;

switch_to_blog( $blog_id );

if( isset( $blogs_template->blog->latest_post->guid ) && !empty( $blogs_template->blog->latest_post->guid) ) {
	
	$latest_post = explode('?p=', $blogs_template->blog->latest_post->guid );
	$post_id = intval( $latest_post[1] );
	
	if( has_post_thumbnail( $post_id ) ) {
	
		$thumbnail_id = get_post_thumbnail_id( $post_id );
		$vignette = wp_get_attachment_image_src( $thumbnail_id, array(50, 50) );
	}
	
} 

if( !$vignette || empty( $vignette[0] ) ){
	
	$args = array(
		'post_type' => 'attachment',
		'numberposts' => 5
	);

	$attachment_ids = array();
	$blog_posts = get_posts( $args );


	foreach( $blog_posts as $attachment ) {
		
		if( strpos( $attachment->post_mime_type, 'image' ) !== false && $attachment->post_parent > 0 ) {
			$attachment_ids[] = $attachment->ID;
		}
	}

	$vignette = wp_get_attachment_image_src( $attachment_ids[0], array(50, 50) );
	
}

restore_current_blog();

if( !empty( $vignette[0] ) )
	return '<img src="'. $vignette[0] .'" alt="Blog avatar" class="avatar blog-1-avatar" width="'.$vignette[1].'" height="'.$vignette[1].'">';

else
	return $avatar;

}