問題
advanced custom fieldのテーブルを出力するには?
解決策
「get_field」で取得し、テーブルのheaderは○○['header']、bodyは○○['body']で、それぞれをforeachで回し、□□['c']で各セルを取得して出力できます。
advanced custom fieldのテーブル
カスタムフィールドを生成するadvanced custom fieldのテーブルはとても便利です。
そのadvanced custom fieldのテーブルを出力する方法をご紹介します。
get_fieldで取得し、[‘header’]と[‘body’]のさらに中の[‘c’]で各セルのデータを取得、表示
「get_field」で取得し、テーブルのheaderは○○[‘header’]、bodyは○○[‘body’]で、それぞれをforeachで回し、□□[‘c’]で各セルを取得して出力できます。
$table = get_field( 'table' );
echo '<table>';
if ( $table['header'] ) {
echo '<thead>';
echo '<tr>';
foreach ( $table['header'] as $th ) {
echo '<th scope="col">';
echo nl2br($th['c']);
/*
echo $th['c'];
*/
echo '</th>';
}
echo '</tr>';
echo '</thead>';
}
echo '<tbody>';
foreach ( $table['body'] as $tr ) {
echo '<tr>';
foreach ( $tr as $td ) {
if( $td == reset( $tr ) ){
echo '<th scope="row">';
echo nl2br($td['c']);
echo '</th>';
}else{
echo '<td>';
echo nl2br($td['c']);
echo '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
コメント