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><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 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 里就行,以[短代码]的形式调用即可。

转载于:https://my.oschina.net/ajian2014/blog/297543

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值