DFdou's Blog Life is short,Be yourself.

7kankan11bookekzwС˵Ҫ · uawx鶼Ķ ðƴӢ ޴½
302/093

WordPress-Shortcodes

If you want to see more,plz click http://www.smashingmagazine.com/2009/02/02/mastering-wordpress-shortcodes/ to see all.
Introduced in WordPress 2.5, shortcodes are powerful but still yet quite unknown WordPress functions. Imagine you could just type “adsense” to display an AdSense ad or “post_count” to instantly find out the number of posts on your blog.

WordPress shortcodes can do this and more and will definitely make your blogging life easier. In this article, we’ll show you how to create and use shortcodes, as well as provide killer ready-to-use WordPress shortcodes that will enhance your blogging experience.
What Are Shortcodes?

Using shortcodes is very easy. To use one, create a new post (or edit an existing one), switch the editor to HTML mode and type a shortcode in brackets, such as:

[showcase]

It is also possible to use attributes with shortcodes. A shortcode with attributes would look something like this:

[showcase id='5']


Shortcodes can also embed content, as shown here:

[url href='http://www.smashingmagazine.com']Smashing Magazine[/url]

Shortcodes are handled by a set of functions introduced in WordPress 2.5 called the Shortcode API. When a post is saved, its content is parsed, and the shortcode API automatically transforms the shortcodes to perform the function they’re intended to perform.

Creating a Simple Shortcode

The thing to remember with shortcodes is that they’re very easy to create. If you know how to write a basic PHP function, then you already know how to create a WordPress shortcode. For our first one, let’s create the well-known “Hello, World” message.

1. Open the functions.php file in your theme. If the file doesn’t exists, create it.
2. First, we have to create a function to return the “Hello World” string. Paste this in your functions.php file:

      function hello() {
          return 'Hello, World!';
      }

3. Now that we have a function, we have to turn it into a shortcode. Thanks to the add_shortcode() function, this is very easy to do. Paste this line after our hello() function, then save and close the functions.php file:

      add_shortcode('hw', 'hello');

The first parameter is the shortcode name, and the second is the function to be called.
4. Now that the shortcode is created, we can use it in blog posts and on pages. To use it, simply switch the editor to HTML mode and type the following:

      [hw]

You’re done! Of course, this is a very basic shortcode, but it is a good example of how easy it is to create one.

Some Related Posts

Comments (3) Trackbacks (0)
  1. yo, nwhy.org great name for site)))
    ————————
    ads: http://sudeg.ru/

    [Reply]

  2. nwhy.org – cooooolest domain name)))
    ————————
    my blog: http://lolej.ru/

    [Reply]

  3. it’s so lucky to register this domain :kua

    [Reply]


Leave a comment

:zhuang :xizao :wuwu :wenhao :wc :touxiang :shuaya :shuajian :shengtian :paopao :no :meinv :maonv :loveu :leilei :kua :jrjr :dahan :chi :chaocai :byebye


CommentLuv Enabled

No trackbacks yet.