Title: Array_Chunk issue in sidebar using shortcode
Last modified: February 1, 2017

---

# Array_Chunk issue in sidebar using shortcode

 *  [mathewemoore](https://wordpress.org/support/users/mathewemoore/)
 * (@mathewemoore)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/array_chunk-issue-in-sidebar-using-shortcode/)
 * I have a shortcode for displaying REST posts and i would like to use the array_chunk
   function to split into new rows at x number of posts. My code below works fine
   in pages, posts and in the sidebar. However, since this isn’t a return; the code
   causes the main div to break out of the text widget and pushes the widget title
   below it.
 * Is there a way to return the output so this can be used effectively in the sidebar?
 *     ```
       if( !empty( $posts ) ) {  
   
       		 echo "<div class='mrp-container'>\n"; 
       		 foreach (array_chunk($posts, $a['columns']) as $post) {
   
       			echo "<div class='mrp-row'>\n";
   
           		foreach ( $post as $post ) {
       				// Make JSON date human readable
       				$jsondate = new DateTime( $post->date );
       				$humandate = $jsondate->format("F d, Y");
               		echo '<div class="single-mrp-post'.$mrpcolumns.'">' . '<a href="' . $post->link. '" title="'. $post->title->rendered . 
       				  PHP_EOL . PHP_EOL. strip_tags($post->excerpt->rendered) .' "><img src="'. $post->swp_thumbnail .'"/><h4 class="mrp-post-title">'. $post->title->rendered . '</h4></a><p class="mrp-post-date">'.$humandate."</p></div>\n";
   
       			}
           	echo "</div>\n";
       		}
       		echo "</div>\n";
   
       	 }
       ```
   

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/array_chunk-issue-in-sidebar-using-shortcode/#post-8727168)
 * The usual method is to collect output into a single variable which can be returned.
 *     ```
       $output = '';
       foreach ( $posts as $post ){
          $output .= /* whatever content using $post created here */;
       }
       return $output;
       ```
   
 * When faced with a a bunch of mixed PHP and HTML from an existing snippet, converting
   it all to collect into a single variable can be quite an endeavor. Fortunately,
   there is an alternative. [Output Buffering](http://php.net/manual/en/ref.outcontrol.php).
   Start the buffering at the beginning, then use ob_get_contents() to get the buffered
   content to be returned. No need to alter the output code at all. Don’t forget
   to end and clean the buffer when you’re done.
 *  Thread Starter [mathewemoore](https://wordpress.org/support/users/mathewemoore/)
 * (@mathewemoore)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/array_chunk-issue-in-sidebar-using-shortcode/#post-8727526)
 * Most graciously appreciate you help with this. That’s exactly what i needed. 
   I added the ob_end_clean(); to the end like you suggested. And everything works
   perfect, no more breaking out of the widget sidebar. Thanks a million!
 * Here’s my final working code:
 *     ```
       ob_start ();
           if( !empty( $posts ) ) {  
   
       	echo "<div class='mrp-container'>\n"; 
       	foreach (array_chunk($posts, $a['columns']) as $post) {
   
       	echo "<div class='mrp-row'>\n";
   
           	foreach ( $post as $post ) {
       	// Make JSON date human readable
       	$jsondate = new DateTime( $post->date );
       	$humandate = $jsondate->format("F d, Y");
               echo '<div class="single-mrp-post'.$mrpcolumns.'">' . '<a href="' . $post->link. '" title="'. $post->title->rendered . 
       	PHP_EOL . PHP_EOL. strip_tags($post->excerpt->rendered) .' "><img src="'. $post->swp_thumbnail .'"/><h4 class="mrp-post-title">'. $post->title->rendered . '</h4></a><p class="mrp-post-date">'.$humandate."</p></div>\n";
       	        }
           	echo "</div>\n";
       	}
       	echo "</div>\n";
       	$output = ob_get_contents();
       }
       ob_end_clean();
       return $output;
       ```
   
    -  This reply was modified 9 years, 2 months ago by [mathewemoore](https://wordpress.org/support/users/mathewemoore/).

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Array_Chunk issue in sidebar using shortcode’ is closed to new replies.

## Tags

 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [rest](https://wordpress.org/support/topic-tag/rest/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [mathewemoore](https://wordpress.org/support/users/mathewemoore/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/array_chunk-issue-in-sidebar-using-shortcode/#post-8727526)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
