前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress 技巧:使用 Shortcode 快速插入表格

WordPress 技巧:使用 Shortcode 快速插入表格

作者头像
Denis
发布2023-04-15 11:20:40
3630
发布2023-04-15 11:20:40
举报
文章被收录于专栏:WordPress果酱

在 WordPress 后台代码模式下输入表格,总有一种想死的感觉,要输入 <table> <tbody> <tr> <td> 等一堆代码,看着头都晕,实在是受不了了,所以就写了下面这个插件,使用 Shortcode 快速插入表格。

这个插件很简单,它主要依靠通过以下规则来生成一个表格:

  • 连续两个回车一行(tr)。
  • 一个回车一个单元格(td)。

如以下的代码:

代码语言:javascript
复制
[table width="95%"]
位置
大小
价格
出现的页面

顶部广告位
728X80 静态图片
1000 RMB 一个月
整站所有页面

侧边栏广告位
300X150 静态图片
500 RMB 一个月
整站所有页面,最多三个
[/table]

就会生成如下的表格:

位置

大小

价格

出现的页面

顶部广告位

728X80 静态图片

1000 RMB 一个月

整站所有页面

侧边栏广告位

300X150 静态图片

500 RMB 一个月

整站所有页面,最多三个

将下面的代码复制到当前主题的 functions 文件中,或者直接保存一个插件,并上传激活即可。

代码语言:javascript
复制
<?php
/*
Plugin Name: WPJAM Table Shortcode
Plugin URI: http://blog.wpjam.com/m/wordpress-shortcode-for-table/
Description: 使用 Shortcode 快速输入表格。
Version: 0.1
Author: Denis
*/
add_shortcode( 'table', 'wpjam_table_shortcode_handler' );
function wpjam_table_shortcode_handler( $atts, $content='' ) {
    extract( shortcode_atts( array(
        'border'        => '1',
        'cellpading'    => '0',
        'cellspacing'   => '0',
        'width'         => ''
    ), $atts ) );

    $output = '';

    $trs = explode("\r\n\r\n", $content);

    foreach($trs as $tr){
        $tr = trim($tr);

        if($tr){
            $tds = explode("\r\n", $tr);
            $output .= '

<tr>';
            foreach($tds as $td){
                $td = trim($td);
                if($td){
                    $output .= '

<td>'.$td.'</td>
';
                }
            }
            $output .= '</tr>
';
        }
    }

    if($class){
        $class = ' class="'.$class.'"';
    }

    if($width){
        $width = ' width="'.$width.'"';
    }

    $output = '

<table border="'.$border.'" cellpading="'.$cellpading.'" cellspacing="'.$cellspacing.'" '.$width.' '.$class.' >'.$output.'</table>
';

    return $output;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档