Advanced Custom Fieldsにテーブルのフィールドタイプを追加できるプラグイン

Filed under: wordpressプラグイン — kdcs @ 25年8月21日 木曜日

プラグインは「Advanced Custom Fields: Table Field」

テンプレートへのテーブル出力方法

ループ出力の中で使用

最小限の構成(th・td)での出力

<?php		
$table = get_field( 'your_table_field_name' );
if ( ! empty ( $table ) ) {
echo '<table>';
  echo '<tbody>';
    foreach ( $table['body'] as $tr ) {
    echo '<tr>';
      foreach ( $tr as $td ) {
      echo '<td>';
        echo $td['c'];
        echo '</td>';
      }
      echo '</tr>';
    }
    echo '</tbody>';
  echo '</table>';
}
?>

基本の出力例(キャプションとテーブルヘッダーも出力する)

<?php
$table = get_field( 'your_table_field_name' );
if ( ! empty ( $table ) ) {
echo '<table border="0">';
  if ( ! empty( $table['caption'] ) ) {
  echo '<caption>' . $table['caption'] . '</caption>';
  }
  if ( ! empty( $table['header'] ) ) {
  echo '<thead>';
    echo '<tr>';
      foreach ( $table['header'] as $th ) {
      echo '<th>';
        echo $th['c'];
        echo '</th>';
      }
      echo '</tr>';
    echo '</thead>';
  }
  echo '<tbody>';
    foreach ( $table['body'] as $tr ) {
    echo '<tr>';
      foreach ( $tr as $td ) {
      echo '<td>';
        echo $td['c'];
        echo '</td>';
      }
      echo '</tr>';
    }
    echo '</tbody>';
  echo '</table>';
}
?>

サイト内検索

カテゴリー

最近の投稿

« |Advanced Custom Fieldsにテーブルのフィールドタイプを追加できるプラグイン| »
↑上に戻る