我知道 WordPress 有神奇的自定義函式,這個自定義函式基本上可以代替大部分的外掛了;我也知道 WordPress 有神奇的自定義域,雖然用
起來比較麻煩,但是不能不說它真的很強大;今天我才發現,原來 WordPress 還有更神奇的短程式碼 (簡碼) 的功能!當然,還是得藉助
function.php 這個檔案來實現。

什麼是短程式碼 (簡碼)?就是你在寫日誌的時候,在編輯器裡隨便插入幾個英文字元,當出現在文章裡的時候,它就是千變萬化的其他內容了。你說神奇 不?!

一、超連結用 [url]

1. 開啟主題中的 functions.php 檔案。貼上以下函式到其中:

 

function myUrl($atts, $content = null) {
extract(shortcode_atts(array(
"href" => 'http://'
), $atts));
return '<a href="http://'/spanspan./spanspan$href/spanspan./spanspan'">'.$content.'</a>';
}
add_shortcode("url", "myUrl");//把函式轉化成簡碼

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 檔案得爆炸了……不知道會不會對速度有影響。