• 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

    (@bcworkz)

    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. 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

    (@mathewemoore)

    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 8 years, 1 month ago by mathewemoore.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Array_Chunk issue in sidebar using shortcode’ is closed to new replies.