how can I display the first post in a single colum

2019-08-25 18:40发布

问题:

I currently have all the posts displayed in one column:

1
2
3
...

I'd like to achieve something similar:

1
23
4
56
...

does anyone have any ideas of how I could do this? is it possible? thank you so much in advance :)

right now I have:

<?php if( $wp_query->current_post <= 0 ) : ?>
code for the first one column post
<?php else : ?>
the rest of the posts styled in columns
<?php endif; ?>

回答1:

The solution I found for this problem was using the $wp_query->current_post of a default loop, in a filter function added to functions.php file in the theme, when post's div is post_class()

function special_recurrent_post_class($classes) {
      if( is_home() ) {
      global $wp_query;
      $extra_classes = array('','specific','specific','');
      $classes[] = $extra_classes[$wp_query->current_post%count($extra_classes)];
      }
      return $classes;
}
add_filter('post_class', 'special_recurrent_post_class');

The example above restricts this to posts on the home page or posts page; otherwise, you need to change the coditional tag is_home() to something else.



回答2:

Just use a counter like this:

// before loop
$ctr = 1; 

if ($ctr == 1) {
    *** code for one column; ***
    $ctr ++;
} else {
    *** code for two column; ***
    $ctr++
    if ($ctr == 4) $ctr = 1   // Reset the counter, back to one column
}