• Hi
    I am trying to make my custo theme, and looking for last post highlight, by searching I found two solutions, and would like to know which one is better. For both I am using them in the index file.

    Solution A

    <?php
    while( have_posts() ): the_post();
    if($i==0): $column_xs = 12; $column_sm = 12; $class = ' fz-recent-post';
    elseif($i > 0 && $i <= 2): $column_xs = 12; $column_sm = 6; $class = ' fz-old-posts';
    elseif($i > 2): $column = 4; $class = ' third-row-padding';
    endif;
    ?>
    	<div class="col-xs-<?php echo $column_xs; ?> col-sm-<?php echo $column_sm; echo $class; ?> extra-class">
    	Post info (i.e. title, excerpt)
    	</div>
    <?php $i++; endwhile; ?>

    Solution B

    <?php
    $args = array(
    	'post_type' => 'post',
    	'posts_per_page' => 1
    );
    $query = new WP_query ( $args );
    if ( $query->have_posts() ) { 
    while ( $query->have_posts() ) : $query->the_post(); ?>
    	<div class="row">
    	Post info (i.e. title, excerpt)
    	</div>
    <?php endwhile;
    wp_reset_postdata(); ?>
    
    <?php
    $args = array(
    	'post_type' => 'post',
    	'offset' => 1
    );
    $query = new WP_query ( $args );
    if ( $query->have_posts() ) { 
    while ( $query->have_posts() ) : $query->the_post(); ?>
    	<div class="row">
    	Post info (i.e. title, excerpt)
    	</div>
    <?php endwhile;
    wp_reset_postdata(); 
    } ?>

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • I haven’t checked the code works, I’m assuming it does – but from a quick glance I would say the first option is better because the second option creates two loops when you only need to loop through the posts once. Better load time/performance.

    Thread Starter keneso

    (@keneso)

    Thank you.
    I thought so, but confirmation from who knows better is always good.

    Yes they both work, with my knowledge the second allows more layout flexibility, but I guess I’ll opt for performance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Post highlighting solutiuons’ is closed to new replies.