Advertisement
5ally

Untitled

Mar 12th, 2019
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. <!-- START of WP Query -->
  2. <?php
  3. $current_page = max( 1, get_query_var( 'paged' ) );
  4. $the_query = new WP_Query( array(
  5.     'post_type'      => 'post',
  6.     'posts_per_page' => 10,
  7.     'paged'          => $current_page,
  8.     // other args here
  9. ) );
  10.  
  11. if ( $the_query->have_posts() ) :
  12.     $count = 1;
  13.     $posts = [];
  14.  
  15.     foreach ( $the_query->posts as &$post ) {
  16.         $class = '';
  17.         switch ( true ) {
  18.             case ( 1 === $count ): $class = 'item1'; break;
  19.             case ( $count < 5 ):   $class = 'item2'; break;
  20.             case ( 9 === $count ): $class = 'item3'; break;
  21.             // other conditions here
  22.             default:               $class = 'item4'; break;
  23.         }
  24.  
  25.         if ( ! isset( $posts[ $class ] ) )
  26.             $posts[ $class ] = [];
  27.  
  28.         $posts[ $class ][] = $post;
  29.         $count++;
  30.     }
  31.  
  32.     $count = 1;
  33.     foreach ( $posts as $class => $post_arr ) :
  34.     ?>
  35.         <div class="container">
  36.             <?php
  37.             foreach ( $post_arr as $post ) :
  38.                 setup_postdata( $post );
  39.                 ?>
  40.                     <div class="item <?php echo $class; ?>">
  41.                         <span>Post <?php echo $count; ?></span>
  42.                         <?php the_title( '<h3>', '</h3>' ); ?>
  43.                     </div>
  44.                 <?php
  45.                 $count++;
  46.             endforeach; // end $post_arr
  47.             ?>
  48.         </div>
  49.     <?php
  50.     endforeach; // end $posts
  51. ?>
  52. <style><!-- DEMO STYLES -->
  53. .container { border: 1px solid #ccc; padding: 10px; margin: 10px; }
  54. .item {
  55.     display: inline-block;
  56.     margin: 0 10px 10px -3px;
  57.     padding: 10px;
  58.     width: 25%;
  59.     vertical-align: top;
  60. }
  61. .item1 { background: red; color: #fff; }
  62. .item2 { background: orange; color: #fff; }
  63. .item3 { background: yellow; color: #333; }
  64. .item4 { background: green; color: #fff; }
  65. </style>
  66.  
  67. <p>Pagination:</p>
  68. <?php
  69.     $big = 999999999; // need an unlikely integer
  70.     echo paginate_links( array(
  71.         'base'     => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
  72.         'format'   => '?paged=%#%',
  73.         'current'  => $current_page,
  74.         'total'    => $the_query->max_num_pages,
  75.         'type'     => 'list',
  76.         'end_size' => 3,
  77.     ) );
  78. ?>
  79. <?php else : ?>
  80.     <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
  81. <?php endif;
  82.  
  83. wp_reset_postdata();
  84. ?>
  85. <!-- END of WP Query -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement