Shortcode

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn

Since Version 2.5 WordPress support so called Shortcodes. They have been introduced for creating macros to be use in a posts content. For examples of shortcodes and how to use them, see WordPress.com Shortcodes, though some shortcodes featured are exclusive to WordPress.com.

A trivial shortcode for a gallery looks like this:

[gallery]

You can also print a shortcode directly in a template like so:

 <?php echo do_shortcode('[gallery]'); ?>

Shortcodes can be with additional attributes as the following example shows:

[gallery id="123" size="medium"]

Both examples will display an image gallery which would be hard to maintain when writing the HTML markup for it and keeping it in sync with uploaded images.


Built-In and Additional Shortcodes

WordPress does offer some of the shortcodes by default (for example the gallery one) and plugins can add their own as well via the Shortcode API.