Skip to content

Instantly share code, notes, and snippets.

@INDIAN2020
Created May 1, 2013 14:15
Show Gist options
  • Select an option

  • Save INDIAN2020/5495493 to your computer and use it in GitHub Desktop.

Select an option

Save INDIAN2020/5495493 to your computer and use it in GitHub Desktop.
wordpress: table shortcode
function simple_table( $atts ) {
extract( shortcode_atts( array(
'cols' => 'none',
'data' => 'none',
), $atts ) );
$cols = explode(',',$cols);
$data = explode('<data>',$data);
$total = count($cols);
$output .= '<table class="mytable"><tr class="th">';
foreach($cols as $col):
$output .= '<td>'.$col.'</td>';
endforeach;
$output .= '</tr><tr>';
$counter = 1;
foreach($data as $datum):
$output .= '<td bgcolor="#f1f1f1">'.$datum.'</td>';
if($counter%$total==0):
$output .= '</tr>';
endif;
$counter++;
endforeach;
$output .= '</table>';
return $output;
}
add_shortcode( 'table', 'simple_table' );
/*usage [table cols="names,values" data="name1,25,name2,409"] */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment