public
Last active

WP_Query_Columns - Columns for the loop. - http://wordpress.stackexchange.com/q/9308/178

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
<?php
 
/**
* get_columns_array
*
* Columns for the loop, single function interface (limited)
*
* Copyright (c) 2011 hakre <http://hakre.wordpress.com/>, some rights reserved
*
* USAGE:
*
* foreach(get_columns_array($post_count) as $column_count) :
* // column starts here
* while ($column_count--) : $the_query->the_post();
* // output your post
* endwhile;
* // column ends here
* endforeach;
*
* @author hakre <http://hakre.wordpress.com/>
* @see http://wordpress.stackexchange.com/q/9308/178
*/
function get_columns_array($totalCount, $columnSize) {
$columns = array();
$totalCount = (int) max(0, $totalCount);
if (!$count)
return $columns;
$columnSize = (int) max(0, $columnSize);
if (!$columnSize)
return $columns;
($floor = (int) ($totalCount / $columnSize))
&& $columns = array_fill(0, $floor, $columnSize)
;
($remainder = $totalCount % $columnSize)
&& $columns[] = $remainder
;
return $columns;
}
 
/**
* WP_Query_Columns
*
* Columns for the loop.
*
* Copyright (c) 2011 hakre <http://hakre.wordpress.com/>, some rights reserved
*
* @author hakre <http://hakre.wordpress.com/>
* @see http://wordpress.stackexchange.com/q/9308/178
*/
class WP_Query_Columns implements Countable, IteratorAggregate {
/**
* column size
* @var int
*/
private $size;
private $index = 0;
private $query;
public function __construct(WP_Query $query, $size = 10) {
$this->query = $query;
$this->size = (int) max(0, $size);
}
/**
* @return WP_Query
*/
public function query() {
return $this->query;
}
private function fragmentCount($fragmentSize, $totalSize) {
$total = (int) $totalSize;
$size = (int) max(0, $fragmentSize);
if (!$total || !$size)
return 0;
$count = (int) ($total / $size);
$count * $size != $total && $count++;
return $count;
}
private function fragmentSize($index, $fragmentSize, $totalSize) {
$index = (int) max(0, $index);
if (!$index)
return 0;
$count = $this->fragmentCount($fragmentSize, $totalSize);
if ($index > $count)
return 0;
return $index === $count ? ($totalSize - ($count-1) * $fragmentSize) : $fragmentSize;
}
public function columnSize($index) {
return $this->fragmentSize($index, $this->size, $this->query->post_count);
}
/**
* @see Countable::count()
* @return int number of columns
*/
public function count() {
return $this->fragmentCount($this->size, $this->query->post_count);
}
/**
* @return array
*/
public function columns() {
$count = $this->count();
$array = $count ? range(1, $count) : array();
return array_map(array($this, 'columnSize'), $array);
}
/**
* @see IteratorAggregate::getIterator()
* @return traversable columns
*/
public function getIterator() {
return new ArrayIterator($this->columns());
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.