Shortcodes, HTML tables, and multiple rows

pass your data in single variables delimited by some char:

[myproduct cols="name,quantity,price" data="name1,5,2.00,name2,3,3.25"]

then explode it into an array and output. I didn’t bother with table markup here, but you get the idea:

function myproduct_func( $atts ) {
    extract( shortcode_atts( array(
        'cols' => 'none',
        'data' => 'none',
    ), $atts ) );

    $cols = explode(',',$cols);
    $data = explode(',',$data);
    $total = count($cols);

    $output = "";
    foreach($cols as $col):
        $output .= "| {$col} ";
    endforeach;

    $output .= "<br>";

    $counter = 1;
    foreach($data as $datum):
        $output .= "| {$datum} ";
        if($counter%$total==0):
            $output .= "<br>";
        endif;
        $counter++;
    endforeach;

    return $output;

}
add_shortcode( 'myproduct', 'myproduct_func' );

Leave a Comment