neilgee
8/13/2015 - 6:07 AM

Column Grid Genesis Refactored

Column Grid Genesis Refactored

<?php
//Know the code https://hub.wpdevelopersclub.com/podcast/post-column-grid-patterns-part-2-episode-3/
add_filter( 'post_class', 'add_to_post_classes_for_grid_pattern' );
/**
 * Add to the Post Classes
 *
 * @since 1.0.0
 *
 * @param array     $classes
 * @return array
 */
function add_to_post_classes_for_grid_pattern( array $classes ) {
	// We are only targeting the home (Blog) page
	if ( ! is_home() ) { //tagetting home - otherwise set other conditional
		return $classes;
	}

	return get_classes_for_grid_pattern( $classes, 3 );
	
}

/*Based on the number of columns reuired, get the styling classes for the grid pattern*/
function get_classes_for_grid_pattern( array $classes, $number_of_columns = 2 ) {

	if ( $number_of_columns <  2 || $number_of_columns >  6  ) {
	return $classes;
	}

	// This is the global instance of the WP_Query Loop query
	global $wp_query;


	$column_classes = array(
	'',             // index 0
	'',             // index 1
	'one-half',     // index 2 = represents 2 columns
	'one-third',    // index 3
	'one-fourth',   // index 4
	'one-fifth',    // index 5
	'one-sixth',    // index 6
	);

	//copy the column class out of the array and add it to the end of the $classes array
	$classes[] = $column_classes [ $number_of_columns ];

	//remainder = 0 gives us the first in a row class
	if($wp_query->current_post % $number_of_columns == 0) {
		$classes[] = 'first';
	}

	// Don't forget to return the classes to code that is firing the event.
	return $classes;
}