wordpress短代码转php,七个超级实用的WordPress 短代码

WordPress短代码(Shortcodes)十分灵活,使用简单,简单到只需要在文章中插件这些短代码即可。今天,跟大家分享10个超级有用的WordPress 短代码,希望可以激发你的创意。

显示网站缩略图

推荐酷站或者展示友情链接的时候,配上一张网站的页面的缩略图是不是更酷,实现起来很简单,只需把下面的代码复制到主题中的functions.php 文件:function wpr_snap($atts, $content = null) {

extract(shortcode_atts(array(

"snap" => 'http://s.wordpress.com/mshots/v1/',

"url" => 'http://www.catswhocode.com',

"alt" => 'My image',

"w" => '400', // width

"h" => '300' // height

), $atts));

$img = '' . $alt . '';

return $img;

}

add_shortcode("snap", "wpr_snap");

之后在需要显示的页面添加如下所示的短代码,就会显示down.chinaz.com的缩略图:[snap url="http://www.catswhocode.com" alt="My description" w="400" h="300"]

添加Paypal 捐赠链接

很多人都通过在博客添加Paypal 捐赠链接向支持者寻求帮助,接下来这个短代码可以在页面上显示一个Paypal 捐赠按钮,还是将下面的代码复制到 functions.php 文件:function cwc_donate_shortcode( $atts ) {

extract(shortcode_atts(array(

'text' => 'Make a donation',

'account' => 'REPLACE ME',

'for' => '',

), $atts));

global $post;

if (!$for) $for = str_replace(" ","+",$post->post_title);

return ''.$text.'';

}

add_shortcode('donate', 'cwc_donate_shortcode');

Source: http://blue-anvil.com/archives/8-fun-useful-shortcode-functions-for-wordpress/

保护邮件地址

众所周知,经常有人扫描网页收集邮箱地址用于群发邮件,没人愿意接收垃圾邮件,但是又需要在博客上显示邮件地址怎么办呢?这些代码将创建一个短代码,为你解决这个问题。照样,还是复制下面的代码到functions.php 文件:function cwc_mail_shortcode( $atts , $content=null ) {

for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "" . ord($content[$i]) . ';';

return ''.$encodedmail.'';

}

add_shortcode('mailto', 'cwc_mail_shortcode');

引用短代码:[mailto]email@yourdomain.com[/mailto]

创建私密内容

如果你想创建一些私密内容仅注册用户可见I,下面的代码可以解决这个问题。 复制代码至 functions.php 文件以创建短代码:function cwc_member_check_shortcode( $atts, $content = null ) {

if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )

return $content;

return '';

}

add_shortcode( 'member', 'cwc_member_check_shortcode' );

引用短代码:[member]This text will be only displayed to registered users.[/member]

只显示在Feed的内容

这个短代码允许创建只在Feed中显示的内容,适合发布Rss广告等情形使用,复制下面的代码至 functions.php文件,哈,你也猜到了吗?function cwc_feedonly_shortcode( $atts, $content = null) {

if (!is_feed()) return "";

return $content;

}

add_shortcode('feedonly', 'cwc_feedonly_shortcode');

然后,就可以运用如下形式的短代码来插入只显示在Feed的内容了。[feedonly]Dear RSS readers, please visit my website and click on a few ads[/feedonly]

显示最后上传的图片附件

WordPress的附件管理可真让人头疼,插入图片一点都不方便,但这个短代码可以解决这些烦恼,能帮你在文章中引用最后一个上传的图片附件,当然还是复制代码至 functions.php:function cwc_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 '

'.$fullimage.'
';

}

add_shortcode("postimage", "cwc_postimage");

完成之后,插件图片链接就变成插入下面的短代码了:[postimage]

插入RSS源

这个短代码允许你在文章中插入任何类型的RSS源码,特别适合于展示其它博客时使用,复制以下代码至 functions.php 文件:include_once(ABSPATH.WPINC.'/rss.php');

function cwc_readRss($atts) {

extract(shortcode_atts(array(

"feed" => 'http://',

"num" => '1',

), $atts));

return wp_rss($feed, $num);

}

add_shortcode('rss', 'cwc_readRss');

然后就可以使用下面的短代码来插入RSS源:[rss feed="http://feeds.feedburner.com/catswhocode" num="5"]

WordPress下载

软件大小:6.28MB

软件类别:国外软件 | 博客系统

软件语言:简体中文

运行环境:PHP/Mysql

软件授权:免费版

更新时间:2013-7-24 16:05:30

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值