I have code that work’s perfectly for the desktop view & mobile view. But the code is not best practice because its have duplication. Here is my code:

<div class="hidden-sm">  
            <?php wp_reset_query(); ?>

            <?php
             $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
             $args = array(
              'post_type' => 'post',
                  'posts_per_page' => 4,
                  'paged' => $paged,
                  'tax_query' => array(
                                  array(                
                                      'taxonomy' => 'post_format',
                                      'field' => 'slug',
                                      'terms' => array('post-format-video'),
                                      'operator' => 'IN')
                                      )
                );
             $wp_query = new WP_Query( $args ); if ( $wp_query->have_posts() ) : ?>


              <div class="row">
                 <?php $count=0; ?> 
                  <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
                  <?php get_template_part( 'template/loop', 'video' ); ?> 

                  <?php $count++; ?>

                  <?php if ($count==4 ||$wp_query->found_posts==0) : 

                      echo '</div><div class="row ">';

                  ?>

                  <?php $count=0; ?>

                  <?php endif; ?>

                  <?php endwhile; ?>


                  <?php if ($wp_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
                  <div class="clearfix"></div>
                  <div class="pagination-sec">
                    <?php echo get_previous_posts_link( '<button type="button" class="btn btn-tazkirah btn-page"><i class="fa fa-chevron-left" aria-hidden="true"></i> Sebelumnya</button>' ); // display newer posts link ?>
                    <?php echo get_next_posts_link( '<button type="button" class="btn btn-tazkirah btn-page">Seterusnya <i class="fa fa-chevron-right" aria-hidden="true"></i> </button>', $wp_query->max_num_pages ); // display older posts link ?>
                  </div>  
                  <?php } ?>

                <?php else : ?>             
                    <h3>Sorry but there are no portfolio items.</h3>
                <?php endif; ?>

                <?php wp_reset_query(); ?>
            </div> 
         </div>

         <!-- End Loop 1 -->   

         <div class="visible-sm">  
            <?php wp_reset_query(); ?>

            <?php
             $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
             $args = array(
              'post_type' => 'post',
                  'posts_per_page' => 4,
                  'paged' => $paged,
                  'tax_query' => array(
                                  array(                
                                      'taxonomy' => 'post_format',
                                      'field' => 'slug',
                                      'terms' => array('post-format-video'),
                                      'operator' => 'IN')
                                      )
                );
             $wp_query = new WP_Query( $args ); if ( $wp_query->have_posts() ) : ?>


              <div class="row">
                 <?php $count=0; ?> 
                  <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
                  <?php get_template_part( 'template/loop', 'video' ); ?> 

                  <?php $count++; ?>

                  <?php if ($count==2 ||$wp_query->found_posts==0) : 

                      echo '</div><div class="row">';

                  ?>

                  <?php $count=0; ?>

                  <?php endif; ?>

                  <?php endwhile; ?>


                  <?php if ($wp_query->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
                  <div class="clearfix"></div>
                  <div class="pagination-sec">
                    <?php echo get_previous_posts_link( '<button type="button" class="btn btn-tazkirah btn-page"><i class="fa fa-chevron-left" aria-hidden="true"></i> Sebelumnya</button>' ); // display newer posts link ?>
                    <?php echo get_next_posts_link( '<button type="button" class="btn btn-tazkirah btn-page">Seterusnya <i class="fa fa-chevron-right" aria-hidden="true"></i> </button>', $wp_query->max_num_pages ); // display older posts link ?>
                  </div>  
                  <?php } ?>

                <?php else : ?>             
                    <h3>Sorry but there are no portfolio items.</h3>
                <?php endif; ?>

                <?php wp_reset_query(); ?>
            </div> 
         </div>

I was tried to combine & simplified the code, but it’s not success. Anyone can help me simplified the code? Thanks in advanced!

Read more here: How to simplified this loop code?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: