ob_get_contents, ob_end_clean Khối nổi (ok)

function getpostblogs() {
  ob_start();
  global $post;
  $index = 1;
  $args = array('numberposts'=>5);
  $custom_posts = get_posts($args);
  echo '<div class="postblogwrap uk-flex">';
    foreach($custom_posts as $post) : setup_postdata($post);
      $linkp = get_permalink();
      if($index == 1) {
        echo '<div class="postone">';
          echo '<a href="'.$linkp.'">'.get_the_post_thumbnail(get_the_ID(),"large").'</a>';
          echo '<a href="'.$linkp.'"><h3>'.get_the_title().'</h3></a>';
          the_excerpt();
        echo '</div>';
        echo '<div class="postmanywrap">';
      }else {
        echo '<div class="postmany">';
          echo '<a class="uk-flex" href="'.$linkp.'">'.get_the_post_thumbnail(get_the_ID(),"thumbnail").'<h3>'.get_the_title().'</h3></a>';
        echo '</div>';
        if($index == 5) {
          echo '</div>';
        }
      }
    $index++;
    endforeach;
    wp_reset_postdata();
  echo '</div>';
  $data = ob_get_contents();
  ob_end_clean();
  return $data;
}
add_shortcode( 'post_shortcode', 'getpostblogs' );

https://e-web.vn/cac-ham-ob_start-ob_get_contents-ob_clean-ob_end_flush-la-gi-dung-de-lam-gi/

ob_start();
echo "Hieu.dev. ";
echo "From e-web.vn";
$content1 = ob_get_contents();
ob_clean();
ob_end_flush();
echo $content1; // Hieu.dev. From e-web.vn
 
ob_start();
echo "The quick brown fox Jumps over the lazy dog.";
$content2 = ob_get_contents();
ob_clean();
ob_end_flush();
 
echo $content2; // The quick brown fox Jumps over the lazy dog.

Last updated