Php 在WordPress中创建不带插件或代码的HTML表

Php 在WordPress中创建不带插件或代码的HTML表,php,wordpress,Php,Wordpress,我希望我的网站用户能够创建无需使用代码或插件表 最好在theme的function.php中编写函数 例如: 如果用户输入 [table 4] c1, c2, c3, c4, c5, c6 [end table] 我希望表标记中的数字4表示表中的列数。每一个昏迷都表示每个细胞的结束 我在主题的functions.php中尝试了一个replace函数。它帮助了我,但是用户必须按照下面的方式编写代码,这并不理想 [table] [row] [cell] c1 [/cell] [cell] c2 [

我希望我的网站用户能够创建无需使用代码或插件表

最好在theme的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]

您可以使用Wordpress内部的短代码来实现这一点。添加它们相对简单(请参见)。在主题的
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]

您可以使用Wordpress内部的短代码来实现这一点。添加它们相对简单(请参见)。在主题的
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]

好吧,你可以很容易地编写你的短代码来实现这一点。你已经试过什么了?而且,在Wordpress中,快捷码属性不是这样工作的;您需要指定如下内容:
[table cols=“4”]
。您可以很容易地编写您的短代码来实现这一点。你已经试过什么了?而且,在Wordpress中,快捷码属性不是这样工作的;您需要指定如下内容:
[table cols=“4”]