当前位置: 非凡设计 > CMS教程 > WordPress > 正文内容

八段神奇的wordpress短代码-WordPress

http://www.feisi.net发布时间:2010-04-23来源:未知 阅读: 收藏到QQ书签

一、超链接用[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列表形式显示日志。

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

  使用代码:[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文件得爆炸了……不知道会不会对速度有影响。

------分隔线----------------------------
Photoshop抠图教程:教你如何使用魔术棒抠图
Photoshop抠图教程:
ps教程:如何设计冷艳色调的质感图片
ps教程:如何设计冷艳
ps文字特效:制作水晶文字效果
ps文字特效:制作水晶
使用photoshop软件制作常见的投射光线
使用photoshop软件制
Photoshop教程:美女照片转成铅笔效果
Photoshop教程:美女
Photoshop调出怀旧的熏黄色调
Photoshop调出怀旧的
SA Onorato 包装设计欣赏
SA Onorato 包装设计
99个极富创意的logo设计作品
99个极富创意的logo设
2010年Web设计风赏析
2010年Web设计风赏析
模仿到创新:Web 设计中的中国风赏析(上)
模仿到创新:Web 设计
LOGO设计作品赏析
LOGO设计作品赏析
55个激发灵感的黑白网页设计欣赏
55个激发灵感的黑白网