我知道 WordPress 有神奇的自定義函數,這個自定義函數基本上可以代替大部分的插件了;我也知道 WordPress 有神奇的自定義域,雖然用
起來比較麻煩,但是不能不説它真的很強大;今天我才發現,原來 WordPress 還有更神奇的短代碼 (簡碼) 的功能!當然,還是得藉助
function.php 這個文件來實現。
什麼是短代碼 (簡碼)?就是你在寫日誌的時候,在編輯器裏隨便插入幾個英文字符,當出現在文章裏的時候,它就是千變萬化的其他內容了。你説神奇 不?!
一、超鏈接用 [url]
1. 打開主題中的 functions.php 文件。粘貼以下函數到其中:
function myUrl($atts, $content = null) {
|
2. 簡碼創建成功,現在就可在日誌和頁面上使用了。
[url href=「http://www.WordPress.com」]WordPress recipes[/url]
日誌保存後,簡碼會顯示名為 「WordPress recipes」 的鏈接,並指向 http://www.WordPress.com 。
代碼註釋:若要正常運行,簡碼必須處理兩個參數:$atts 和
$content 。 $atts 是簡碼屬性,上例中,屬性為 href,且包括了 URL 鏈接。 $content 是簡碼內容,位於域名和子目錄之間 (即
www.example.com 和 「/subdirectory」 之間) 。正如以上顯示,我們給 $atts 和 $content 都設置了默認值。
二、創建 「發送到 twitter」 的簡碼
| function twitt() { return '<div id="twitit"><a href="http://twitter.com/home?status=Currently%20reading%20'/spanspan./spanspanget_permalink/spanfont%20color="#000000">($post->ID).'" title="Click to send this page to Twitter!" target="_blank">Share on Twitter</a></div>'; } add_shortcode('twitter', 'twitt'); |
然後只要在你文章需要的地方插入 [twitter] 此簡碼,「發送到 Twitter」 鏈接就會只出現放置簡碼的位置。
三、創建 「RSS 訂閲」 簡碼
| function subscribeRss() { return '<div ><a href="http://feed.happyet.org">Enjoyed this post? Subscribe to my RSS feeds!</a></div>'; } add_shortcode('subscribe', 'subscribeRss'); |
同樣用 [subscribe] 就可以顯示了,當然加點 css 修飾一下就更好了。
四、定製 Google AdSense 位置
| function showads() { return '<div id="adsense"> //google adsense code here </div>'; } add_shortcode('adsense', 'showads'); |
使用 [adsense] 在你需要的位置調用 google ad,記得給外包的那個 div 設置 css 樣式。
五、嵌入 RSS 閲讀器
| //This file is needed to be able to use the wp_rss() function. include_once(ABSPATH.WPINC.'/rss.php'); function readRss($atts) { extract(shortcode_atts(array( "feed" => 'http://', "num" => '1', ), $atts)); return wp_rss($feed, $num); } add_shortcode('rss', 'readRss'); |
使用簡碼的時候輸入:[rss feed=「http://feed.happyet.org」 num=「5」]
feed 屬性 (attribute) 即是要嵌入的 feed URL,num 即是要顯示的條目數量。
六、使用簡碼從 WordPress 數據庫中提取文章
|
function sc_liste($atts, $content = null) {
extract(shortcode_atts(array( "num" => '5', "cat" => '' ), $atts)); global $post; $myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat); $retour='<ul>'; foreach($myposts as $post) : setup_postdata($post); $retour.='<li><a href="http://'/spanspan./spanspanget_permalink/spanspan()./spanspan'">'.the_title("","",false).'</a></li>'; endforeach; $retour.='</ul> '; return $retour; } add_shortcode("list", "sc_liste");
|
在 WordPress 編輯器中使用以下簡碼:[liste num=「3」 cat=「1」],系統將從 ID 為 1 的類別中提取 3 篇文章。
代碼註釋:系統提取參數並創建全局變量 $posts 後,sc_liste() 函數使用了 get_posts(),numberposts,
order, orderby 和 category 參數以從類別 Y 中獲取 X 篇最新日誌。完成後,系統就會以無序的 HTML 列表形式顯示日誌。
七、獲取日誌中的最新圖像
|
function sc_postimage($atts, $content = null) {
extract(shortcode_atts(array( "size" => 'thumbnail', "float" => 'none' ), $atts)); $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() ); foreach( $images as $imageID => $imagePost ) $fullimage = wp_get_attachment_image($imageID, $size, false); $imagedata = wp_get_attachment_image_src($imageID, $size, false); $width = ($imagedata[1]+2); $height = ($imagedata[2]+2); return '<div style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'</div>'; } add_shortcode("postimage", "sc_postimage");
|
使用代碼:[postimage size=「」 float=「left」]
代碼註釋:sc_postimage() 函數首先提取了簡碼屬性,然後它使用 get_children(),
wp_get_attachment_image()
和 wp_get_attachment_image_src() 這些 WordPress 函數檢索圖像。完成後,系統就會返回圖像並插入到文章內容中。
八、在側邊欄微件中添加簡碼
add_filter('widget_text', 'do_shortcode');
好是好,function.php 文件得爆炸了……不知道會不會對速度有影響。