Skip to content

Instantly share code, notes, and snippets.

@kostiantyn-petlia
Last active October 4, 2017 13:02
Show Gist options
  • Save kostiantyn-petlia/6ebb6b2ea57df654feadbd172a76e7e8 to your computer and use it in GitHub Desktop.
Save kostiantyn-petlia/6ebb6b2ea57df654feadbd172a76e7e8 to your computer and use it in GitHub Desktop.
Function: Maximum integer divider
// Find an maximum integer divider. Emxample: max_int_divider(18, 4) => 3; 18/3 = 6 (integer);
// Author - K
// Version 1.01
function max_int_divider( $value = null, $limit = null ) {
// Wrong params
if ( $value === null || ! is_int( $value ) || empty( $limit ) || ! is_int( $limit ) ) {
return false;
}
// Find an maximum integer divider
for ( $i = $limit; $i > 0; $i -- ) {
if ( $value % $i == 0 ) {
return $i;
}
}
// The integer divider didn't found
return false;
}
// How use it ------------------------------------------------------------------------------------------------------------------------
<?php
// Build the query
$the_query = new WP_Query( array(
'post_parent' => get_the_ID(),
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => - 1,
'post_status' => 'publish'
) );
// How we can divide the row in columns
$count = count( $childrens->posts );
$divider = max_int_divider ( $count, 4 ); // Set 4 as the maximum number of columns
$cols = ( $count && $divider ) ? (int)12/$divider : 12; // Columns width for the grid classes (1-12)
// You can check more conditions, like ($count > 1 && $divider == 1) and do something
?>
<?php if ( $the_query->have_posts() ) : ?>
<section class="section section-some">
<div class="row">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="columns small-12 large-<?php echo $cols; ?>">
<div class="content"><?php the_content(); ?></div>
</div>
<?php endwhile; ?>
</div>
</section><!-- .section-some -->
<?php endif; ?>
<?php wp_reset_query(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment