Show date/time ago with latest network posts

Hi.
I am using this code below in a plugin, to show the recent posts on the network on my network in a shortcode. Ash from WPMU Dev actually created the code.
The code is already showing blog name and post title, but now i want to show a date from when the post is posted, or time since it was posted.
Is it possible to do out of the box?

Website: http://www.bloggerspoint.dk
Code:

<?php

class recentproshortcode {

	var $build = 1;

	var $db;

	function __construct() {

		global $wpdb;

		$this->db =& $wpdb;

		if($this->db->blogid == 1) {
			// Only add the feed for the main site
			add_action('init', array(&$this, 'initialise_recentproshortcode') );
		}

		add_shortcode( 'globalproposts', array( &$this, 'display_pro_posts_shortcode') );

	}

	function recentproshortcode() {
		$this->__construct();
	}

	function initialise_recentproshortcode() {
		// In case we need it in future :)
	}

	function display_pro_posts($pro_level, $tmp_number,$tmp_title_characters = 0,$tmp_content_characters = 0,
		$tmp_title_content_divider = '<br />',$tmp_title_before,$tmp_title_after,$tmp_global_before,
		$tmp_global_after,$tmp_before,$tmp_after,$tmp_title_link = 'no',$tmp_show_avatars = 'yes',
		$tmp_avatar_size = 16, $posttype = 'post', $output = true) {

		global $network_query, $network_post, $wpdb;

// https://premium.wpmudev.org/forums/topic/restricting-specific-page-templates-within-prosites#post-652329

		$network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $tmp_number ));

		$html = '';

		global $wpdb;
				$blog_id = $wpdb->blogid;
				$sql = "SELECT level FROM {$wpdb->base_prefix}pro_sites WHERE blog_ID = '$blog_id'";
				$level = $wpdb->get_var( $sql );
				if( $pro_level == 'all' ){
					$pro = $wpdb->get_results("SELECT * FROM {$wpdb->base_prefix}pro_sites");
				}else{
					$pro = $wpdb->get_results("SELECT * FROM {$wpdb->base_prefix}pro_sites WHERE level = {$pro_level}");
				}

				$temp = array();
				foreach( $pro as $p ){
					array_push( $temp, $p->blog_ID );
				}

	//		if ( $level == 2 ) {

			$html .= $tmp_global_before;

				while( network_have_posts() ) {

				network_the_post();

				if( ! in_array( $network_post->BLOG_ID, $temp ) ) continue;

				switch_to_blog( $network_post->BLOG_ID );

				//Get network blog post featured image
				$n_post = network_get_post();
				//	}
				$featured_image = get_the_post_thumbnail( $n_post->ID, 'misc-thumb-front' );
				//Network blog name
				$blogname = get_blog_option( $network_post->BLOG_ID, 'blogname' );

				restore_current_blog();
				$html .= $tmp_before;

					$html .= $tmp_title_before . '<a href="' . network_get_permalink()  . '"target="_blank" class="crop">'; 

					$the_title = network_get_the_title();

						$html .=  '<div id="npost-meta"><div class="blog-name">' . $blogname . '</div> <hr> <div id="net-post-title">' . substr($the_title,0,$tmp_title_characters) . '</div></div>';

		        $tile_img = '';

		        if(!empty($featured_image)) {
		        	$tile_img =  $featured_image;

		        } else {
		        	$options = get_option( 'bpb_settings' );
	        	$tile_img = '<img src="' . plugins_url( '/images/wparm33w.png', dirname(__FILE__) ) .  '" >';
	        	$tile_img = '<img src="' .  get_option( $bpb_placehold_it , plugins_url( '/images/wparm33w.png', dirname(__FILE__) ) ).  '" >';
	        	$tile_img = '<img src="' . $options['bpb_btn_6'] .  '" >';
			        } 

				if ( $tmp_title_characters > 0 ) {
					$html .=  $tile_img;

					$html .= $tmp_title_after;
				}

				$html .= $tmp_after;

			}
			$html .= $tmp_global_after;
//		}

		if($output) {
			echo $html;
		} else {
			return $html;
		}

	}

	function display_pro_posts_shortcode($atts, $content = null, $code = "") {

		$defaults = array(	'number'	=>	50,
							'title_characters' => 35,
							'content_characters' => 50,
							'title_content_divider' => '<br />',
							'title_before'	=>	'<div>',
							'title_after'	=>	'</div>',
							'global_before'	=>	'<ul>',
							'global_after'	=>	'</ul>',
							'before'	=>	'<li class="crop-square">',
							'after'	=>	'</a></li>',
							'title_link' => 'yes',
							'show_avatars' => 'no',
							'show_images' => 'no',
							'avatar_size' => 16,
							'posttype' => 'post',
							'pro_level' => 'all'
						);

		extract(shortcode_atts($defaults, $atts));

		$html = '';

		$html .= $this->display_pro_posts( $pro_level, $number, $title_characters, $content_characters, $title_content_divider, $title_before, $title_after, $global_before, $global_after, $before, $after, $title_link, $show_avatars, $avatar_size, $posttype, false);

		return $html;

	}

}

function display_pro_posts($pro_level, $tmp_number,$tmp_title_characters = 0,$tmp_content_characters = 0,$tmp_title_content_divider = '<br />',$tmp_title_before,$tmp_title_after,$tmp_global_before,$tmp_global_after,$tmp_before,$tmp_after,$tmp_title_link = 'no',$tmp_show_avatars = 'yes', $tmp_avatar_size = 16, $posttype = 'post', $output = true) {
	global $recentproshortcode;

	$recentproshortcode->display_pro_posts( $pro_level, $tmp_number, $tmp_title_characters, $tmp_content_characters, $tmp_title_content_divider, $tmp_title_before, $tmp_title_after, $tmp_global_before, $tmp_global_after, $tmp_before, $tmp_after, $tmp_title_link, $tmp_show_avatars, $tmp_avatar_size, $posttype, $output );
}

$recentproshortcode = new recentproshortcode();
  • Vinod Dalvi

    Hi Mathias,

    I hope you are well today and thank you for your question.

    You can achieve this using the following updated code.

    <?php
    class recentproshortcode {
    
    	var $build = 1;
    
    	var $db;
    
    	function __construct() {
    
    		global $wpdb;
    
    		$this->db =& $wpdb;
    
    		if($this->db->blogid == 1) {
    			// Only add the feed for the main site
    			add_action('init', array(&$this, 'initialise_recentproshortcode') );
    		}
    
    		add_shortcode( 'globalproposts', array( &$this, 'display_pro_posts_shortcode') );
    
    	}
    
    	function recentproshortcode() {
    		$this->__construct();
    	}
    
    	function initialise_recentproshortcode() {
    		// In case we need it in future :)
    	}
    
    	function display_pro_posts($pro_level, $tmp_number,$tmp_title_characters = 0,$tmp_content_characters = 0,
    		$tmp_title_content_divider = '<br />',$tmp_title_before,$tmp_title_after,$tmp_global_before,
    		$tmp_global_after,$tmp_before,$tmp_after,$tmp_title_link = 'no',$tmp_show_avatars = 'yes',
    		$tmp_avatar_size = 16, $posttype = 'post', $output = true) {
    
    		global $network_query, $network_post, $wpdb;
    
    // https://premium.wpmudev.org/forums/topic/restricting-specific-page-templates-within-prosites#post-652329
    
    		$network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $tmp_number ));
    
    		$html = '';
    
    		global $wpdb;
    				$blog_id = $wpdb->blogid;
    				$sql = "SELECT level FROM {$wpdb->base_prefix}pro_sites WHERE blog_ID = '$blog_id'";
    				$level = $wpdb->get_var( $sql );
    				if( $pro_level == 'all' ){
    					$pro = $wpdb->get_results("SELECT * FROM {$wpdb->base_prefix}pro_sites");
    				}else{
    					$pro = $wpdb->get_results("SELECT * FROM {$wpdb->base_prefix}pro_sites WHERE level = {$pro_level}");
    				}
    
    				$temp = array();
    				foreach( $pro as $p ){
    					array_push( $temp, $p->blog_ID );
    				}
    
    	//		if ( $level == 2 ) {
    
    			$html .= $tmp_global_before;
    
    				while( network_have_posts() ) {
    
    				network_the_post();
    
    				if( ! in_array( $network_post->BLOG_ID, $temp ) ) continue;
    
    				switch_to_blog( $network_post->BLOG_ID );
    
    				//Get network blog post featured image
    				$n_post = network_get_post();
    				//	}
    				$featured_image = get_the_post_thumbnail( $n_post->ID, 'misc-thumb-front' );
    				//Network blog name
    				$blogname = get_blog_option( $network_post->BLOG_ID, 'blogname' );
    
    				restore_current_blog();
    				$html .= $tmp_before;
    
    					$html .= $tmp_title_before . '<a href="' . network_get_permalink()  . '"target="_blank" class="crop">'; 
    
    					$the_title = network_get_the_title();
    
    						$html .=  '<div id="npost-meta"><div class="blog-name">' . $blogname . '</div> <hr> <div id="net-post-title">' . substr($the_title,0,$tmp_title_characters) . '</div><div class="net-post-date">'. date( get_option('date_format'), strtotime($n_post->post_date) ).'</div></div>';
    
    		        $tile_img = '';
    
    		        if(!empty($featured_image)) {
    		        	$tile_img =  $featured_image;
    
    		        } else {
    		        	$options = get_option( 'bpb_settings' );
    	        	$tile_img = '<img src="' . plugins_url( '/images/wparm33w.png', dirname(__FILE__) ) .  '" >';
    	        	$tile_img = '<img src="' .  get_option( $bpb_placehold_it , plugins_url( '/images/wparm33w.png', dirname(__FILE__) ) ).  '" >';
    	        	$tile_img = '<img src="' . $options['bpb_btn_6'] .  '" >';
    			        } 
    
    				if ( $tmp_title_characters > 0 ) {
    					$html .=  $tile_img;
    
    					$html .= $tmp_title_after;
    				}
    
    				$html .= $tmp_after;
    
    			}
    			$html .= $tmp_global_after;
    //		}
    
    		if($output) {
    			echo $html;
    		} else {
    			return $html;
    		}
    
    	}
    
    	function display_pro_posts_shortcode($atts, $content = null, $code = "") {
    
    		$defaults = array(	'number'	=>	50,
    							'title_characters' => 35,
    							'content_characters' => 50,
    							'title_content_divider' => '<br />',
    							'title_before'	=>	'<div>',
    							'title_after'	=>	'</div>',
    							'global_before'	=>	'<ul>',
    							'global_after'	=>	'</ul>',
    							'before'	=>	'<li class="crop-square">',
    							'after'	=>	'</a></li>',
    							'title_link' => 'yes',
    							'show_avatars' => 'no',
    							'show_images' => 'no',
    							'avatar_size' => 16,
    							'posttype' => 'post',
    							'pro_level' => 'all'
    						);
    
    		extract(shortcode_atts($defaults, $atts));
    
    		$html = '';
    
    		$html .= $this->display_pro_posts( $pro_level, $number, $title_characters, $content_characters, $title_content_divider, $title_before, $title_after, $global_before, $global_after, $before, $after, $title_link, $show_avatars, $avatar_size, $posttype, false);
    
    		return $html;
    
    	}
    
    }
    
    function display_pro_posts($pro_level, $tmp_number,$tmp_title_characters = 0,$tmp_content_characters = 0,$tmp_title_content_divider = '<br />',$tmp_title_before,$tmp_title_after,$tmp_global_before,$tmp_global_after,$tmp_before,$tmp_after,$tmp_title_link = 'no',$tmp_show_avatars = 'yes', $tmp_avatar_size = 16, $posttype = 'post', $output = true) {
    	global $recentproshortcode;
    
    	$recentproshortcode->display_pro_posts( $pro_level, $tmp_number, $tmp_title_characters, $tmp_content_characters, $tmp_title_content_divider, $tmp_title_before, $tmp_title_after, $tmp_global_before, $tmp_global_after, $tmp_before, $tmp_after, $tmp_title_link, $tmp_show_avatars, $tmp_avatar_size, $posttype, $output );
    }
    
    $recentproshortcode = new recentproshortcode();

    Best Regards,
    Vinod Dalvi

  • Vinod Dalvi

    Hi Mathias,

    To display how long time ago since the posts was posted you can try using the following code.

    <?php
    class recentproshortcode {
    
    	var $build = 1;
    
    	var $db;
    
    	function __construct() {
    
    		global $wpdb;
    
    		$this->db =& $wpdb;
    
    		if($this->db->blogid == 1) {
    			// Only add the feed for the main site
    			add_action('init', array(&$this, 'initialise_recentproshortcode') );
    		}
    
    		add_shortcode( 'globalproposts', array( &$this, 'display_pro_posts_shortcode') );
    
    	}
    
    	function recentproshortcode() {
    		$this->__construct();
    	}
    
    	function initialise_recentproshortcode() {
    		// In case we need it in future :)
    	}
    
    	function time_elapsed_string($datetime, $full = false) {
    	    $now = new DateTime;
    	    $ago = new DateTime($datetime);
    	    $diff = $now->diff($ago);
    
    	    $diff->w = floor($diff->d / 7);
    	    $diff->d -= $diff->w * 7;
    
    	    $string = array(
    	        'y' => 'year',
    	        'm' => 'month',
    	        'w' => 'week',
    	        'd' => 'day',
    	        'h' => 'hour',
    	        'i' => 'minute',
    	        's' => 'second',
    	    );
    	    foreach ($string as $k => &$v) {
    	        if ($diff->$k) {
    	            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
    	        } else {
    	            unset($string[$k]);
    	        }
    	    }
    
    	    if (!$full) $string = array_slice($string, 0, 1);
    	    return $string ? implode(', ', $string) . ' ago' : 'just now';
    	}
    
    	function display_pro_posts($pro_level, $tmp_number,$tmp_title_characters = 0,$tmp_content_characters = 0,
    		$tmp_title_content_divider = '<br />',$tmp_title_before,$tmp_title_after,$tmp_global_before,
    		$tmp_global_after,$tmp_before,$tmp_after,$tmp_title_link = 'no',$tmp_show_avatars = 'yes',
    		$tmp_avatar_size = 16, $posttype = 'post', $output = true) {
    
    		global $network_query, $network_post, $wpdb;
    
    // https://premium.wpmudev.org/forums/topic/restricting-specific-page-templates-within-prosites#post-652329
    
    		$network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $tmp_number ));
    
    		$html = '';
    
    		global $wpdb;
    				$blog_id = $wpdb->blogid;
    				$sql = "SELECT level FROM {$wpdb->base_prefix}pro_sites WHERE blog_ID = '$blog_id'";
    				$level = $wpdb->get_var( $sql );
    				if( $pro_level == 'all' ){
    					$pro = $wpdb->get_results("SELECT * FROM {$wpdb->base_prefix}pro_sites");
    				}else{
    					$pro = $wpdb->get_results("SELECT * FROM {$wpdb->base_prefix}pro_sites WHERE level = {$pro_level}");
    				}
    
    				$temp = array();
    				foreach( $pro as $p ){
    					array_push( $temp, $p->blog_ID );
    				}
    
    	//		if ( $level == 2 ) {
    
    			$html .= $tmp_global_before;
    
    				while( network_have_posts() ) {
    
    				network_the_post();
    
    				if( ! in_array( $network_post->BLOG_ID, $temp ) ) continue;
    
    				switch_to_blog( $network_post->BLOG_ID );
    
    				//Get network blog post featured image
    				$n_post = network_get_post();
    				//	}
    				$featured_image = get_the_post_thumbnail( $n_post->ID, 'misc-thumb-front' );
    				//Network blog name
    				$blogname = get_blog_option( $network_post->BLOG_ID, 'blogname' );
    
    				restore_current_blog();
    				$html .= $tmp_before;
    
    					$html .= $tmp_title_before . '<a href="' . network_get_permalink()  . '"target="_blank" class="crop">'; 
    
    					$the_title = network_get_the_title();
    
    						$html .=  '<div id="npost-meta"><div class="blog-name">' . $blogname . '</div> <hr> <div id="net-post-title">' . substr($the_title,0,$tmp_title_characters) . '</div><div class="net-post-date">'. $this->time_elapsed_string($n_post->post_date).'</div></div>';
    
    		        $tile_img = '';
    
    		        if(!empty($featured_image)) {
    		        	$tile_img =  $featured_image;
    
    		        } else {
    		        	$options = get_option( 'bpb_settings' );
    	        	$tile_img = '<img src="' . plugins_url( '/images/wparm33w.png', dirname(__FILE__) ) .  '" >';
    	        	$tile_img = '<img src="' .  get_option( $bpb_placehold_it , plugins_url( '/images/wparm33w.png', dirname(__FILE__) ) ).  '" >';
    	        	$tile_img = '<img src="' . $options['bpb_btn_6'] .  '" >';
    			        } 
    
    				if ( $tmp_title_characters > 0 ) {
    					$html .=  $tile_img;
    
    					$html .= $tmp_title_after;
    				}
    
    				$html .= $tmp_after;
    
    			}
    			$html .= $tmp_global_after;
    //		}
    
    		if($output) {
    			echo $html;
    		} else {
    			return $html;
    		}
    
    	}
    
    	function display_pro_posts_shortcode($atts, $content = null, $code = "") {
    
    		$defaults = array(	'number'	=>	50,
    							'title_characters' => 35,
    							'content_characters' => 50,
    							'title_content_divider' => '<br />',
    							'title_before'	=>	'<div>',
    							'title_after'	=>	'</div>',
    							'global_before'	=>	'<ul>',
    							'global_after'	=>	'</ul>',
    							'before'	=>	'<li class="crop-square">',
    							'after'	=>	'</a></li>',
    							'title_link' => 'yes',
    							'show_avatars' => 'no',
    							'show_images' => 'no',
    							'avatar_size' => 16,
    							'posttype' => 'post',
    							'pro_level' => 'all'
    						);
    
    		extract(shortcode_atts($defaults, $atts));
    
    		$html = '';
    
    		$html .= $this->display_pro_posts( $pro_level, $number, $title_characters, $content_characters, $title_content_divider, $title_before, $title_after, $global_before, $global_after, $before, $after, $title_link, $show_avatars, $avatar_size, $posttype, false);
    
    		return $html;
    
    	}
    
    }
    
    function display_pro_posts($pro_level, $tmp_number,$tmp_title_characters = 0,$tmp_content_characters = 0,$tmp_title_content_divider = '<br />',$tmp_title_before,$tmp_title_after,$tmp_global_before,$tmp_global_after,$tmp_before,$tmp_after,$tmp_title_link = 'no',$tmp_show_avatars = 'yes', $tmp_avatar_size = 16, $posttype = 'post', $output = true) {
    	global $recentproshortcode;
    
    	$recentproshortcode->display_pro_posts( $pro_level, $tmp_number, $tmp_title_characters, $tmp_content_characters, $tmp_title_content_divider, $tmp_title_before, $tmp_title_after, $tmp_global_before, $tmp_global_after, $tmp_before, $tmp_after, $tmp_title_link, $tmp_show_avatars, $tmp_avatar_size, $posttype, $output );
    }
    
    $recentproshortcode = new recentproshortcode();

    Best Regards,
    Vinod Dalvi

  • Vinod Dalvi

    Hi Mathias,

    Yes it's possible but developing custom code for custom functionality is beyond the scope of support that we provide though we help to develop small code.

    See more information about it here https://premium.wpmudev.org/manuals/using-wpmu-dev/support-vs-custom-development/

    If you are not a programmer then you can consider hiring a developer from https://premium.wpmudev.org/wpmu-custom-development/ to develop it for you.

    Thanks,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.