最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 网站综合 > CMS系统技巧

wordpress更神奇的8段短代码技巧

日期:03-01    来源:中国设计秀    作者:cnwebshow.com

 qi1中国设计秀

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

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

一、超链接用[url]qi1中国设计秀

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

function myUrl($atts, $content = null) {
extract(shortcode_atts(array(
"href" => 'http://'
), $atts));
return '<a href="'.$href.'">'.$content.'</a>';
}
add_shortcode("url", "myUrl");//把函数转化成简码
qi1中国设计秀

2. 简码创建成功,现在就可在日志和页面上使用了。qi1中国设计秀

[url href=“http://www.wordpress.com”]wordPress recipes[/url]qi1中国设计秀

日志保存后,简码会显示名为“wordPress recipes”的链接,并指向http://www.wordpress.com。qi1中国设计秀

代码注释:若要正常运行,简码必须处理两个参数:$atts 和 $content。$atts是简码属性,上例中,属性为href,且包括了URL链接。$content是简码内容,位于域名和子目录之间(即www.example.com和“/subdirectory”之间)。正如以上显示,我们给$atts 和 $content都设置了默认值。qi1中国设计秀

二、创建“发送到 twitter” 的简码qi1中国设计秀

function twitt() {qi1中国设计秀
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>';qi1中国设计秀
}qi1中国设计秀
add_shortcode('twitter', 'twitt');
qi1中国设计秀

然后只要在你文章需要的地方插入[twitter]此简码,“发送到Twitter”链接就会只出现放置简码的位置。qi1中国设计秀

三、创建“RSS订阅”简码qi1中国设计秀

function subscribeRss() {qi1中国设计秀
return '<div class="rss-box"><a href="http://feed.happyet.org">Enjoyed this post? Subscribe to my RSS feeds!</a></div>';qi1中国设计秀
}qi1中国设计秀
add_shortcode('subscribe', 'subscribeRss');
qi1中国设计秀

同样用[subscribe]就可以显示了,当然加点css修饰一下就更好了。qi1中国设计秀

四、定制Google AdSense位置qi1中国设计秀

function showads() {qi1中国设计秀
return '<div id="adsense">qi1中国设计秀
//google adsense code hereqi1中国设计秀
</div>';qi1中国设计秀
}qi1中国设计秀
add_shortcode('adsense', 'showads');
qi1中国设计秀

使用[adsense]在你需要的位置调用google ad,记得给外包的那个div设置css样式。qi1中国设计秀

五、嵌入RSS阅读器qi1中国设计秀

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

使用简码的时候输入:[rss feed=“http://feed.happyet.org” num=“5”]qi1中国设计秀

feed属性(attribute)即是要嵌入的feed URL,num即是要显示的条目数量。qi1中国设计秀

六、使用简码从wordPress数据库中提取文章qi1中国设计秀

function sc_liste($atts, $content = null) {qi1中国设计秀
extract(shortcode_atts(array(qi1中国设计秀
"num" => '5',qi1中国设计秀
"cat" => ''qi1中国设计秀
), $atts));qi1中国设计秀
global $post;qi1中国设计秀
$myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat);qi1中国设计秀
$retour='<ul>';qi1中国设计秀
foreach($myposts as $post) :qi1中国设计秀
setup_postdata($post);qi1中国设计秀
$retour.='<li><a href="'.get_permalink().'">'.the_title("","",false).'</a></li>';qi1中国设计秀
endforeach;qi1中国设计秀
$retour.='</ul> ';qi1中国设计秀
return $retour;qi1中国设计秀
}qi1中国设计秀
add_shortcode("list", "sc_liste");
qi1中国设计秀

wordPress编辑器中使用以下简码:[liste num=“3” cat=“1”],系统将从ID为1的类别中提取3篇文章。qi1中国设计秀

代码注释:系统提取参数并创建全局变量$posts后,sc_liste()函数使用了 get_posts(),numberposts, order, orderby和category参数以从类别Y中获取X篇最新日志。完成后,系统就会以无序的HTML列表形式显示日志。qi1中国设计秀

七、获取日志中的最新图像qi1中国设计秀

function sc_postimage($atts, $content = null) {qi1中国设计秀
extract(shortcode_atts(array(qi1中国设计秀
"size" => 'thumbnail',qi1中国设计秀
"float" => 'none'qi1中国设计秀
), $atts));qi1中国设计秀
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() );qi1中国设计秀
foreach( $images as $imageID => $imagePost )qi1中国设计秀
$fullimage = wp_get_attachment_image($imageID, $size, false);qi1中国设计秀
$imagedata = wp_get_attachment_image_src($imageID, $size, false);qi1中国设计秀
$width = ($imagedata[1]+2);qi1中国设计秀
$height = ($imagedata[2]+2);qi1中国设计秀
return '<div class="postimage" style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'</div>';qi1中国设计秀
}qi1中国设计秀
add_shortcode("postimage", "sc_postimage");
qi1中国设计秀

使用代码:[postimage size=“” float=“left”]qi1中国设计秀

代码注释:sc_postimage()函数首先提取了简码属性,然后它使用get_children(), wp_get_attachment_image() 和wp_get_attachment_image_src()这些wordPress函数检索图像。完成后,系统就会返回图像并插入到文章内容中。qi1中国设计秀

八、在侧边栏微件中添加简码qi1中国设计秀

add_filter('widget_text', 'do_shortcode');qi1中国设计秀

好是好,function.php文件得爆炸了……不知道会不会对速度有影响。(原文链接)qi1中国设计秀

本文引用地址:/site/article_63951.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明