Posts from one category divided into two columns


/ Published in: PHP
Save to your folder(s)

Divide wordpress posts from one category into two columns.


Copy this code and paste it in your HTML
  1. <?php
  2. // loop only artists posts
  3. global $post; // required
  4. $category_id = ( get_cat_ID( 'Teksty' ) );
  5.  
  6. $args = array(
  7. 'category' => $category_id,
  8. 'numberposts' => -1,
  9. );
  10. $custom_posts = get_posts($args);
  11. $count = count( $custom_posts );
  12.  
  13. // only if more posts
  14. if( $count > 1) {
  15. // column one
  16. $col1_count = ceil( $count/2 );
  17. $col1 = array_slice( $custom_posts, 0, $col1_count );
  18. // column 2
  19. $col2_count = ($count - $col1_count);
  20. $col2 = array_slice( $custom_posts, -$col2_count );
  21. }
  22. else {
  23. $col1 = &$custom_posts;
  24. $col2 = array();
  25. }
  26.  
  27.  
  28. // col1
  29. ?>
  30. <div id="column-1">
  31. <?php
  32. foreach($col1 as $post) : setup_postdata($post); ?>
  33. <?php include (TEMPLATEPATH . '/inc/text-list-item.php'); ?>
  34. <?php
  35. endforeach;
  36. ?>
  37. </div><!-- #column-1
  38. --><div id="column-2">
  39. <?php
  40. foreach($col2 as $post) : setup_postdata($post); ?>
  41. <?php include (TEMPLATEPATH . '/inc/text-list-item.php'); ?>
  42. <?php
  43. endforeach;
  44. ?>
  45. </div><!-- #column-2 -->

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.