2016-12-18 10 views
0

コードまたはプラグインを使用せずにウェブサイトのユーザーがテーブルを作成できるようにしたい。プラグインまたはコードなしでWordPressでHTMLテーブルを作成する

テーマのfunction.phpに関数を書くことが好ましい。

例: ユーザが

[table 4] 
c1, c2, c3, c4, c5, c6 
[end table] 

に入る場合、私は、テーブルタグ内の数字4、テーブル内の列の数を表現したいです。各コマは各セルの終わりを示す。

テーマのfunctions.phpでreplace関数を試しました。それは私を助けたが、そのためには、以下のようなコードを書く必要があり、これは理想的ではない。

[table] 
[row] 
[cell] c1 [/cell] 
[cell] c2 [/cell] 
[cell] c3 [/cell] 
[cell] c4 [/cell] 
[/row] 
[row] 
[cell] c5 [/cell] 
[cell] c6 [/cell] 
[/row] 
[end table] 
+0

さて、あなたは簡単にそれを達成するために、あなたのショートを書くことができます。あなたは何を試しましたか?また、それはWordpressでshortcode属性がどのように機能するかではありません。 '[table cols =" 4 "]'のようなものを指定する必要があります。 – BenM

+0

ありがとう@BenM – Shareef

答えて

2

これを達成するには、Wordpressのショートコードを使用します。それらを追加するのは比較的簡単です(the docsを参照してください)。次のようにあなたのテーマのfunctions.phpの内部で、このショートを作成するための基本的な実施例は以下のとおりです。次のように

function _my_theme_sc_table($atts, $content) 
{ 
    // Normalise the attributes: 
    $a = shortcode_atts(array(
     'cols' => 4 
    ), $atts); 

    // Now extract the content (will be a CSV of items): 
    $cells = explode(',', $content); 
    $numCells = count($cells); 
    $rows = ceil($numCells/$a['cols']); 

    $html = '<table>'; 
    $html.= ' <tbody>'; 

    for($r = 0; $r < $rows; $r++) 
    { 
     $html.= ' <tr>'; 

     for($c = 0; $c < $a['cols']; $c++) 
     { 
      $index = ($r * $a['cols']) + $c; 
      $html.= '<td>'.(($index < $numCells) ? trim($cells[$index]) : '').'</td>'; 
     } 

     $html.= ' </tr>'; 
    } 

    $html.= ' </tbody>'; 
    $html.= '</table>'; 

    return $html; 

} 
add_shortcode('table', '_my_theme_sc_table'); 

使い方は次のとおりです。

[table cols="4"]c1, c2, c3, c4, c5, c6[/table] 
関連する問題