WordPress 短代码(简码)收藏

WordPress 的短代码对我来说一直都有些神秘,一方面是因为接触地不多,另外是因为短代码没有使用的必要性,而且我还只是 PHP 语言的初学者,所以我一直都是中规中矩地写着普通的 PHP 代码。在本站上唯一涉及 WordPress 短代码的地方就是 Clean Archives Reloaded 的调用方法,不过也没有深入研究是个怎么回事。

前些日子在刷 Google Reader 的时候,看到 happyet 的一篇 WordPress 短代码(简码)收藏,让我大开眼界,原来短代码也有这么多用法,转载收藏一下。

一、超链接用[url]

1. 打开主题中的 functions.php 文件。粘贴以下函数到其中:

function myUrl($atts, $content = null) {
extract(shortcode_atts(array(
"href" => 'http://'
), $atts));
return '<a href="'.$href.'">'.$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 reading '.get_permalink($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 class="rss-box"><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="'.get_permalink().'">'.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 class="postimage" 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');

把以上代码放在 functions.php 里就行,以[短代码]的形式调用即可。

本文转自:WordPress 短代码(简码)收藏

  1. 我说的聚合的意思其实是收集。 :idea:
    就像我现在博客用的那个插件一样,可以收集同步到facebook中的博客的评论。
    不知道说清楚了没有。

  2. 为什么你的博客在我的谷歌阅读器里不能打开的??用代理就能打开。好奇怪哦。。直接访问网址却可以的。抱歉,发生意外情况,阻碍了 Google 阅读器完成请求。

  3. 万戈有不有看过刘淼博客中的品论栏,好像是收集了twitter的RT。我现在用了一个插件,不知道你有不有办法找到非插件聚合评论的方法啊?
    是不是有些难啊? :smile:

    • 没有去过你说的刘淼博客。。。
      非插件的方法肯定有,只是没有理解你说的聚合评论,是指聚合谁的评论呀?