卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

Wordpress短代码教程

我晓得wordpress有神奇的自定义函数,这个自定义函数基本上可以代替大部分的插件了;我也知道wordpress有神奇的自定义域,虽然用 起来比较麻烦,但是不能不说它真的很强大;今天我才发现,原来wordpress除了更神奇的短代码(简码)的功能!当然,还是得借助 function.php这个文件来实现。

什么是长代码(简码)?就是你在写日志的时候,在编辑器里随便插入几个英文字符,当出现在文章里的时候,它就是千变万化的其他内容了。你说神奇 不?!

一、超链接用[url

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

functionmyUrl($atts, $content= null) { extract(shortcode_atts(array( "href"=> 'http://' ), $atts)); return''.$content.''; } 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” 的简码

functiontwitt() { return'ID).'" title="Click to send this page to Twitter!" target="_blank">Share on Twitter
'; } add_shortcode('twitter', 'twitt');

三、创建“RSS订阅”简码然后只要在你文章须要的地方插入[twitter]此简码,“发送到Twitter”链接就可以只出现置放简码的位置。

functionsubscribeRss() { return'Enjoyed this post? Subscribe to my RSS feeds!
'; } add_shortcode('subscribe', 'subscribeRss');

四、定制Google AdSense边线同样用[subscribe]就可以显示了,当然加点css修饰一下就更好了。

functionshowads() { return' //google adsense code here 
'; } add_shortcode('adsense', 'showads');

五、嵌入RSS阅读器采用[adsense]在你须要的位置调用google ad,回忆起给外包的那个div设置CSS样式。

//This file is needed to be able to use the wp_rss() function. include_once(ABSPATH.WPINC.'/rss.php'); functionreadRss($atts) { extract(shortcode_atts(array( "feed"=> 'http://', "num"=> '1', ), $atts)); returnwp_rss($feed, $num); } add_shortcode('rss', 'readRss');

feed属性(attribute)即是要嵌入的feed URL,num即是必须表明的条目数量。采用简码的时候输出:[rss feed=“http://feed.happyet.org” num=“5”]

六、采用简码从WordPress数据库中提取文章

functionsc_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=' '; return$retour; } add_shortcode("list", "sc_liste");

代码注释:系统抽取参数并创建全局变量$posts后,sc_liste()函数使用了 get_posts(),numberposts, order, orderby和category参数以从类别Y中以获取X篇最新日志。顺利完成后,系统就会以无序的HTML列表形式显示日志。在WordPress编辑器中采用以下简码:[liste num=“3” cat=“1”],系统将从ID为1的类别中抽取3篇文章。

七、获取日志中的最新图像

functionsc_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( $imagesas$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''.$fullimage.''; } add_shortcode("postimage", "sc_postimage");

代码注释:sc_postimage()函数首先提取了简码属性,然后它使用get_children(), wp_get_attachment_image() 和wp_get_attachment_image_src()这些WordPress函数检索图像。完成后,系统就可以返回图像并填入到文章内容中。使用代码:[postimage size=“” float=“left”]

八、在侧边栏微件中添加简码

add_filter('widget_text', 'do_shortcode');

好就是好,function.php文件得核爆了……不晓得可以不会对速度有影响。

卓越飞翔博客
上一篇: WordPress相关文章的几种方法
下一篇: Ecshop二次开发后台订单实现一键发货功能教程

相关推荐

留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏