wordpress画像数に応じてページ送りさせたい
下記のコードは記事の中にある画像(サムネイル以外)を全て表示させ、表示された画像が16枚を超えたら
ページ送りをするものになる予定でした。
このコードで本当に画像が16枚を超えたらページ送りするものの予定でしたが、
これですと記事の中に画像が20枚あったら20枚全部表示されてさらに他の記事も表示されないと
ページ送りしてくれません。
どうしたら解決するでしょうか?
ちなみにこちらのサイトを参考にしました。
http://kachibito.net/wordpress/custom/stacking-posts-in-a-grid.html
<?php 
  $num_cols = 4; // カラム数
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // ページネーション
  $args = array(  
        'posts_per_page' => 16, // 1ページに表示するポスト数
        'cat' => 0, // 表示させたい記事カテゴリのID
        'paged' => $paged
        ); 
    query_posts($args);
  if (have_posts()) :
    for ( $i=1 ; $i <= $num_cols; $i++ ) :
      $counter = $num_cols + 1 - $i;
    while (have_posts()) : the_post();
  if( $counter%$num_cols == 0 ) : ?>
<?php $output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
    $all_img = $matches[1];
if( !empty( $all_img) ) {
    foreach( $all_img as $img ) { ?>
<?php echo '<img src="'.$img.'">' ?>
<?php 
  endif;
  endwhile;
    rewind_posts();
      endfor;//ここからページネーション作成
        next_posts_link('« 前へ');
        previous_posts_link('先へ »');
      else:
      echo 'no posts';
 }
}
      endif; wp_reset_query(); ?>