Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- START of WP Query -->
- <?php
- $current_page = max( 1, get_query_var( 'paged' ) );
- $the_query = new WP_Query( array(
- 'post_type' => 'post',
- 'posts_per_page' => 10,
- 'paged' => $current_page,
- // other args here
- ) );
- if ( $the_query->have_posts() ) :
- $count = 1;
- $posts = [];
- foreach ( $the_query->posts as &$post ) {
- $class = '';
- switch ( true ) {
- case ( 1 === $count ): $class = 'item1'; break;
- case ( $count < 5 ): $class = 'item2'; break;
- case ( 9 === $count ): $class = 'item3'; break;
- // other conditions here
- default: $class = 'item4'; break;
- }
- if ( ! isset( $posts[ $class ] ) )
- $posts[ $class ] = [];
- $posts[ $class ][] = $post;
- $count++;
- }
- $count = 1;
- foreach ( $posts as $class => $post_arr ) :
- ?>
- <div class="container">
- <?php
- foreach ( $post_arr as $post ) :
- setup_postdata( $post );
- ?>
- <div class="item <?php echo $class; ?>">
- <span>Post <?php echo $count; ?></span>
- <?php the_title( '<h3>', '</h3>' ); ?>
- </div>
- <?php
- $count++;
- endforeach; // end $post_arr
- ?>
- </div>
- <?php
- endforeach; // end $posts
- ?>
- <style><!-- DEMO STYLES -->
- .container { border: 1px solid #ccc; padding: 10px; margin: 10px; }
- .item {
- display: inline-block;
- margin: 0 10px 10px -3px;
- padding: 10px;
- width: 25%;
- vertical-align: top;
- }
- .item1 { background: red; color: #fff; }
- .item2 { background: orange; color: #fff; }
- .item3 { background: yellow; color: #333; }
- .item4 { background: green; color: #fff; }
- </style>
- <p>Pagination:</p>
- <?php
- $big = 999999999; // need an unlikely integer
- echo paginate_links( array(
- 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
- 'format' => '?paged=%#%',
- 'current' => $current_page,
- 'total' => $the_query->max_num_pages,
- 'type' => 'list',
- 'end_size' => 3,
- ) );
- ?>
- <?php else : ?>
- <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
- <?php endif;
- wp_reset_postdata();
- ?>
- <!-- END of WP Query -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement