how can I display the first post in a single column and others in 2 columns? wordpress

梦想的初衷 提交于 2019-12-25 05:23:28

问题


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
}


来源:https://stackoverflow.com/questions/11602290/how-can-i-display-the-first-post-in-a-single-column-and-others-in-2-columns-wor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!