Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Simple Grid helper functions.
*
* @package SimpleGrid
* @subpackage Genesis
* @copyright Copyright (c) 2014, Flagship, LLC
* @license GPL-2.0+
* @since 1.0.0
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Add post classes for a simple grid loop.
*
* @since 1.0.0
* @category Grid Loop
* @param $classes array An array of classes that should be appended
* @param $columns int The number of grid items desired
* @return $classes array The grid classes
*/
function prefix_simple_grid( $classes, $columns ) {
global $wp_query;
//* Bail if we don't have a column number or it is invalid.
if ( ! isset( $columns ) || ! in_array( $columns, array( 2, 3, 4, 6 ) ) ) {
return;
}
$classes = array( 'simple-grid' );
$column_classes = array(
2 => 'one-half',
3 => 'one-third',
4 => 'one-fourth',
6 => 'one-sixth'
);
$classes[] = $column_classes[absint($columns)];
//* Add an "odd" class to allow for more control of grid collapse.
if ( ( $wp_query->current_post + 1 ) % 2 ) {
$classes[] = 'odd';
}
if( 0 === $wp_query->current_post || 0 === $wp_query->current_post % $columns ) {
$classes[] = 'first';
}
return $classes;
}
/**
* Set up a grid of one-half elements for use in a post_class filter.
*
* @since 1.0.0
* @category Grid Loop
* @param $classes array An array of the current post classes
* @return $classes array The post classes with the grid appended
*/
function prefix_grid_one_half( $classes ) {
return array_merge( (array) prefix_simple_grid( $classes, 2 ), $classes );
}
/**
* Set up a grid of one-third elements for use in a post_class filter.
*
* @since 1.0.0
* @category Grid Loop
* @param $classes array An array of the current post classes
* @return $classes array The post classes with the grid appended
*/
function prefix_grid_one_third( $classes ) {
return array_merge( (array) prefix_simple_grid( $classes, 3 ), $classes );
}
/**
* Set up a grid of one-fourth elements for use in a post_class filter.
*
* @since 1.0.0
* @category Grid Loop
* @param $classes array An array of the current post classes
* @return $classes array The post classes with the grid appended
*/
function prefix_grid_one_fourth( $classes ) {
return array_merge( (array) prefix_simple_grid( $classes, 4 ), $classes );
}
/**
* Set up a grid of one-sixth elements for use in a post_class filter.
*
* @since 1.0.0
* @category Grid Loop
* @param $classes array An array of the current post classes
* @return $classes array The post classes with the grid appended
*/
function prefix_grid_one_sixth( $classes ) {
return array_merge( (array) prefix_simple_grid( $classes, 6 ), $classes );
}
<?php
add_action( 'genesis_loop', 'prefix_widget_area_helper' );
/**
* Display a widgeted area with a grid loop post class.
*
* @since 1.0.0
*/
function prefix_widget_area_helper() {
add_filter( 'post_class', 'prefix_grid_one_half' );
genesis_widget_area( 'widget-area', array(
'before' => '<div class="widget-area">',
'after' => '</div> <!-- end .widget-are -->',
) );
remove_filter( 'post_class', 'prefix_grid_one_half' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.