dd

wordpress进阶教程(二十七):wordpress实用短代码介绍


短代码的应用一般在一些固定输出内容上,比如在文章中某位置添加一个广告,如果每次都去输入广告代码多麻烦,如果我们直接创建一个短代码 [ashu_ads] 用来输出广告代码,以后给文章中添加广告岂不是相加就加? 又如,网站经常有下载链接,且网站设计中给每个现在链接都设置了固定的css,如果每次添加一个短代码都输入一个如下的代码,烦不烦?

<div class="ashu_down"><a href="http://shouce.ren">点此下载</a></div>   function showads() {    return '我是广告代码,请替换成你的广告代码';    }    add_shortcode('adsense', 'showads');   //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://www.shouce.ren/feed" num="5"]  

加入我们创建了短代码[ashu_down link="http://shouce.ren"]下载文件[/ashu_down],然后每次添加这个即可,是不是很方便?当然这个例子对比不是很明显。

实例一、添加广告短代码

只需在文章中添加一个短代码 [adsense],即插入我们已经写好的谷歌广告代码。

<div class="ashu_down"><a href="http://shouce.ren">点此下载</a></div>   function showads() {    return '我是广告代码,请替换成你的广告代码';    }    add_shortcode('adsense', 'showads');   //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://www.shouce.ren/feed" num="5"]  

你还可以改成输出一些捐赠链接、以及其它社交网站链接。

 

实例二、嵌入RSS阅读器

<div class="ashu_down"><a href="http://shouce.ren">点此下载</a></div>   function showads() {    return '我是广告代码,请替换成你的广告代码';    }    add_shortcode('adsense', 'showads');   //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://www.shouce.ren/feed" num="5"]  

使用是在文章中插入

<div class="ashu_down"><a href="http://shouce.ren">点此下载</a></div>   function showads() {    return '我是广告代码,请替换成你的广告代码';    }    add_shortcode('adsense', 'showads');   //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://www.shouce.ren/feed" num="5"]  

feed属性(attribute)即是要嵌入的feed URL,num即是要显示的条目数量。