Paginating Recent Global Posts

Good plugin but obvious lapses exists.

1. How do I paginate the output?
After displaying X entries, how do I show the remaining entries?

2. How do I add a more link?
Cutting out the content after XY characters isn't a beautiful solution as you find words are cutoff in the middle or sometimes after one character. That isn't good enough for a plugin of this standard.

  • Kasia Swiderska

    Hello Village,

    Recent Global Posts does not have pagination option and option to use more link - adding this would require custom coding and changing code of the plugin I'm afraid.
    But I found one thread where user implemented pagination with custom code and shared solution https://premium.wpmudev.org/forums/topic/recent-network-posts-pagination#post-472303

    You can even modify this code so instead of using php function to cut text after character limit it will use wp_trim_words funtion that will allow to trim text after amount of words - this part:

    $html .= substr(strip_tags($the_content),0,$tmp_content_characters);

    change to this one:

    $html .= wp_trim_words(strip_tags($the_content),$tmp_content_characters);

    Let me know if you need further assistance with this.

    kind regards,
    Kasia

  • Village

    function display_recent_posts($tmp_number,$tmp_title_characters = 0,$tmp_content_characters = 0,$tmp_title_content_divider = '
    ',$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;
    $network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $tmp_number, 'paged' => get_query_var('page')
    ) );
    ?>

    $html = '';

    if( network_have_posts() ) {

    $html .= $tmp_global_before;
    $default_avatar = get_option('default_avatar');

    while( network_have_posts()) {

    network_the_post();
    $posttype = $network_post->post_type;
    $the_content = $network_post->post_content;
    $string = $the_content;

    $html .= $tmp_before;
    $the_title = network_get_the_title();
    if ( $tmp_title_characters > 0 ) {
    $html .= $tmp_title_before;
    if ( $tmp_show_avatars == 'yes' ) {
    $the_author = network_get_the_author_id();
    // $html .= get_avatar( $the_author, $tmp_avatar_size, $default_avatar) . ' ';
    // Change Avatar to desired image source with link
    $html .='<img title="'. $the_title .'" width="160" height="101" class="thumb" src="'. $img_src .'" />';
    }

    if ( $tmp_title_link == 'no' ) {
    $html .= substr($the_title,0,$tmp_title_characters);
    } else {
    $html .= '
    ' . substr($the_title,0,$tmp_title_characters) . '';
    }

    $html .= $tmp_title_after;
    }
    $html .= $tmp_title_content_divider;

    if ( $tmp_content_characters > 0 ) {
    $the_content = network_get_the_content();
    $html .= substr(strip_tags($the_content),0,$tmp_content_characters);
    }
    $html .= $tmp_after;
    }

    }
    $html .= $tmp_global_after;
    // Pagination
    global $page;
    $network_query_count = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => -1, 'paged' => get_query_var('page')) );
    $total_count = count($network_query_count);
    $pages = ceil($total_count / $tmp_number);

    $html .= "<div style='clear: both;'></div>";
    $paginarea = '<div class="paginarea">';
    if (($page) != 1) { //Previous Page Link
    $paginarea .= '<';
    }
    for($i=1;$i<=$pages;$i++) {
    if(($page)==$i) {
    $paginarea .= '<span class="active_page">';
    }
    $paginarea .= '   ';
    $paginarea .= ' ' . $i . ' ';
    $paginarea .= '
    ';
    if(($page)==$i) {
    $paginarea .= '</span>';
    }
    }
    if (($page) < $pages) { //Next Page Link
    $paginarea .= '  >';
    }
    $paginarea .= '</div>';
    echo $paginarea;
    echo "
    ";
    $html .= $paginarea;
    // End Pagination
    if($output) {
    echo $html;
    } else {
    return $html;
    }
    }

    I basically removed some parts I think were specific to the original contributor's.

  • Nithin

    Hi Village,

    Hope you are doing good today. :slight_smile:

    Could you please try the following code, and check whether it works:

    function display_recent_posts($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;
    
    		$network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $tmp_number ));
    
    		$html = '';
    
    		if( network_have_posts() ) {
    			$html .= $tmp_global_before;
    			$default_avatar = get_option('default_avatar');
    
    			while( network_have_posts()) {
    				network_the_post();
    
    				$html .= $tmp_before;
    				if ( $tmp_title_characters > 0 ) {
    					$html .= $tmp_title_before;
    					if ( $tmp_show_avatars == 'yes' ) {
    						$the_author = network_get_the_author_id();
    						$html .= get_avatar( $the_author, $tmp_avatar_size, $default_avatar) . ' ';
    					}
    					$the_title = network_get_the_title();
    					if ( $tmp_title_link == 'no' ) {
    						$html .= substr($the_title,0,$tmp_title_characters);
    					} else {
    						$html .= '<a href="' . network_get_permalink() . '" >' . substr($the_title,0,$tmp_title_characters) . '</a>';
    					}
    
    					$html .= $tmp_title_after;
    				}
    				$html .= $tmp_title_content_divider;
    
    				if ( $tmp_content_characters > 0 ) {
    					$the_content = network_get_the_content();
    					$html .= wp_trim_words(strip_tags($the_content),$tmp_content_characters);
    				}
    				$html .= $tmp_after;
    
    			}
    			$html .= $tmp_global_after;
    		}
    
    		if($output) {
    			echo $html;
    		} else {
    			return $html;
    		}
    
    	}

    I hope this helps. Please let us know how that goes, have a nice day. :slight_smile:

    Kind Regards,
    Nithin

  • Nithin

    Hi Village,

    By the way where is this code supposed to go? functions.php? I did try it and it resulted in Error 500 when I attempted to update the file.

    I guess, it was mentioned in the old thread, but it seems like it isn't, sorry about that. The function display_recent_posts is part of the plugin class, and you'll have to make changes to the plugin file in order to make pagination work. Please go to /wp-content/recent-posts/recent-posts.php, and in line 65, replace the function display_recent_posts with the above code.

    I am attaching the file below, so that you'll only have to replace the file recent-posts.php. Please do note that the file will be overwritten once the plugin is updated, you'll have to manually replace the file once every plugin update. I hope this helps. Please let us know how that goes. Have a nice day. :slight_smile:

    Best Regards,
    Nithin

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.