Skip to content

Instantly share code, notes, and snippets.

@coreyweb
Last active January 3, 2016 07:28
Show Gist options
  • Save coreyweb/8429247 to your computer and use it in GitHub Desktop.
Save coreyweb/8429247 to your computer and use it in GitHub Desktop.
WordPress Table Shortcode
<?php
// this shortcode allows you to add multiple rows and columns in a table
// corey modified from this from a shortcode found here:
// http://wpsnipp.com/index.php/functions-php/shortcode-tables-with-multiple-rows-and-columns/
// example shortcode:
// [specs cols="2" data="Row1 Item1:|Row1 Item2|Row2 Item1:|Row2 Item2"]
// (define # of columns in 'cols' and place your contents in 'data' with a delimiter)
// markup is Bootstrap
function specs_list( $atts ) {
extract( shortcode_atts( array(
'cols' => 'none',
'data' => 'none',
), $atts ) );
$data = explode('|',$data);
$total = $cols;
$output .= '<table class="table table-striped table-bordered table-condensed table-specs"><tbody><tr>';
$counter = 1;
foreach($data as $datum):
$span= '3';
if($counter%$total==0):
$span = '6';
endif;
$output .= '<td class="span'. $span .'">'.$datum.'</td>';
if($counter%$total==0):
$output .= '</tr>';
endif;
$counter++;
endforeach;
$output .= '</tbody></table>';
return $output;
}
add_shortcode( 'specs', 'specs_list' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment