WordPress 技巧:使用 Shortcode 快速插入列表
不知道是不是很多同学和我一样在 WordPress 后台喜欢使用代码模式写日志,总是有种强迫症,感觉使用编辑器会带入无关的代码,自己不能控制所有 🙂 。但是使用代码模式写日志有个不好的地方,就是要创建一个列表的时候,需要输入很多代码或者要按很多次 ul/ol/li 这几个按钮。有没有更方便的方法呢?我们可以使用 WordPress Shortcode 实现快速插入列表:
首先将下面的代码复制到当前主题的 functions 文件中,或者直接保存一个插件,并上传激活:
'0'
    ), $atts ) );
    $lists = explode("\n", $content);
    $ouput = '';
    foreach($lists as $li){
        if(trim($li) != '') {
            $output .= "
<li>{$li}</li>
\n";
        }
    }
    if($type=="order"){
        $output = "
<ol>\n".$output."</ol>
\n";
    }else{
        $output = "
<ul>\n".$output."</ul>
\n";
    }
    return $output;
}然后在后台使用代码模式编辑的日志的时候,通过下面的方法快速插入列表:
[list]
item-a
item-b
item-c
[/list]
每一行为一个元素,默认是无序列表,如果要插入有序列表,加入 type="order" 的属性:
[list type="order"]
item-a
item-b
item-c
[/list]
这样就无需输入 <li></li>,相对方便了很多,当然也有一丝丝的蛋疼。 🙂 
更多 WordPress Shortcode 介绍和使用技巧:
 
				
			








