WordPress一些付费主题中一般都会有短代码。使用非常广泛,很能丰富文章的排版,看上去也很漂亮!使用之前一定要备份,在添加后可能会与其它代码冲突。效果图如下:包含了警示框、禁止框、音乐播放器、下载按钮等多种效果,内有注释,可根据自己的情况增删。

WordPress中几个常用的短代码!-一艘大轮船

使用方法:

1、添加代码核心内容:

//将以下代码放至function.php中<?php后?>前。
//警示
function warningbox($atts, $content=null, $code="") {
$return = '<div class="warning shortcodestyle">';
$return .= $content;
$return .= '</div>';
return $return;
}
add_shortcode('warning' , 'warningbox' );
//禁止
function nowaybox($atts, $content=null, $code="") {
$return = '<div class="noway shortcodestyle">';
$return .= $content;
$return .= '</div>';
return $return;
}
add_shortcode('noway' , 'nowaybox' );
//购买
function buybox($atts, $content=null, $code="") {
$return = '<div class="buy shortcodestyle">';
$return .= $content;
$return .= '</div>';
return $return;
}
add_shortcode('buy' , 'buybox' );
//项目版
function taskbox($atts, $content=null, $code="") {
$return = '<div class="task shortcodestyle">';
$return .= $content;
$return .= '</div>';
return $return;
}
add_shortcode('task' , 'taskbox' );
//音乐播放器
function doubanplayer($atts, $content=null){
extract(shortcode_atts(array("auto"=>'0'),$atts));
return '<embed src="'.get_bloginfo("template_url").'/shortcode/doubanplayer.swf?url='.$content.'&amp;autoplay='.$auto.'" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" width="400" height="30">';
}
add_shortcode('music','doubanplayer');

//下载链接
function downlink($atts,$content=null){
extract(shortcode_atts(array("href"=>'http://'),$atts));
return '<div class="but_down"><a href="'.$href.'" target="_blank"><span>'.$content.'</span></a><div class="clear"></div></div>';
}
add_shortcode('Downlink','downlink');
//flv播放器
function flvlink($atts,$content=null){
extract(shortcode_atts(array("auto"=>'0'),$atts));
return'<embed src="'.get_bloginfo("template_url").'/shortcode/flvideo.swf?auto='.$auto.'&flv='.$content.'" menu="false" quality="high" wmode="transparent" bgcolor="#ffffff" width="560" height="315" name="flvideo" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_cn" />';
}
add_shortcode('flv','flvlink');
//mp3专用播放器
function mp3link($atts, $content=null){
extract(shortcode_atts(array("auto"=>'0',"replay"=>'0',),$atts));
return '<embed src="'.get_bloginfo("template_url").'/shortcode/dewplayer.swf?mp3='.$content.'&amp;autostart='.$auto.'&amp;autoreplay='.$replay.'" wmode="transparent" height="20" width="240" type="application/x-shockwave-flash" />';
}
add_shortcode('mp3','mp3link');

//在线视频
/* 20120616 修复WP 3.4 官方中文版 的问题
function wp_embed_handler_youku( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_youku', '<embed src="http://player.youku.com/player.php/sid/' . esc_attr($matches[1]) . '/v.swf" quality="high" width="620" height="390" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr ); }
wp_embed_register_handler( 'youku', '#http://v.youku.com/v_show/id_(.*?).html#i', 'wp_embed_handler_youku' );

function wp_embed_handler_tudou( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_tudou', '<embed src="http://www.tudou.com/v/' . esc_attr($matches[1]) . '/v.swf" quality="high" width="620" height="390" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr );}
wp_embed_register_handler( 'tudou', '#http://www.tudou.com/programs/view/(.*?)/#i', 'wp_embed_handler_tudou' );

*/

function wp_embed_handler_ku6( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_ku6', '<embed src="http://player.ku6.com/refer/' . esc_attr($matches[1]) . '/v.swf" quality="high" width="620" height="390" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr ); }
wp_embed_register_handler( 'ku6', '#http://v.ku6.com/show/(.*?).html#i', 'wp_embed_handler_ku6' );

function wp_embed_handler_youtube( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_youtube', '<embed src="http://www.youtube.com/v/' . esc_attr($matches[1]) . '?&amp;hl=zh_CN&amp;rel=0" width="620" height="390" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr ); }
wp_embed_register_handler( 'youtube', '#http://youtu.be/(.*?)/#i', 'wp_embed_handler_youtube' );

function wp_embed_handler_56 ($matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_56', '<embed src="http://player.56.com/v_' . esc_attr($matches[1]) . '.swf" quality="high" width="620" height="390" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr ); }
wp_embed_register_handler( '56', '#http://player.56.com/v_(.*?).swf#i', 'wp_embed_handler_56' );

function wp_embed_handler_sohu( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_sohu', '<embed src="http://share.vrs.sohu.com/' . esc_attr($matches[1]) . '/v.swf" quality="high" width="620" height="390" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr ); }
wp_embed_register_handler( 'sohu', '#http://share.vrs.sohu.com/(.*?)/v.swf#i', 'wp_embed_handler_sohu' );

function wp_embed_handler_6cn( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_6cn', '<embed src="http://6.cn/p/' . esc_attr($matches[1]) . '.swf" quality="high" width="480" height="385" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr ); }
wp_embed_register_handler( '6cn', '#http://6.cn/p/(.*?).swf#i', 'wp_embed_handler_6cn' );

function wp_embed_handler_letv( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_letv', '<embed src="http://www.letv.com/player/' . esc_attr($matches[1]) . '.swf" quality="high" width="620" height="390" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr ); }
wp_embed_register_handler( 'letv', '#http://www.letv.com/player/(.*?).swf#i', 'wp_embed_handler_letv' );

function wp_embed_handler_sina( $matches, $attr, $url, $rawattr ) { return apply_filters( 'embed_sina', '<embed src="http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=' . esc_attr($matches[1]) . '/s.swf" quality="high" width="620" height="390" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" allowfullscreen="true" wmode="opaque"></embed>', $matches, $attr, $url, $rawattr ); }
wp_embed_register_handler( 'sina', '#http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=(.*?)/s.swf#i', 'wp_embed_handler_sina' );

2、添加代码的样式

//段代码样式,将以下代码放至style.css
.shortcodestyle{margin:20px 0px;padding: 15px 15px 15px 80px;position: relative;-moz-box-shadow:0px 1px 2px rgba(0, 0, 0, 0.2); -webkit-box-shadow:0px 1px 2px rgba(0, 0, 0, 0.2); box-shadow:0px 1px 2px rgba(0, 0, 0, 0.2);border-radius:5px;-webkit-border-radius:5px;-moz-border-radius:5px;-khtml-border-radius:5px;}
.shortcodestyle a{padding:0px;}
.shortcodestyle a:hover{background:none;border-radius:0px;-webkit-border-radius:0px;-moz-border-radius:0px;-khtml-border-radius:0px;}
.warning{background: url(shortcode/shortcode/bell.png) no-repeat scroll 15px 15px #FFEA97;border: 1px solid #E1B400;color: #9F6F26;}
.warning a{ color:#A84A1E;border-bottom:1px dotted #A84A1E;}
.warning a:hover{color:#A84A1E;border-bottom:1px solid #A84A1E; }
.noway{background: url(shortcode/shortcode/noway.png) no-repeat scroll 15px 15px #EFCEC9;border: 1px solid #BE1F1F;color: #820B0B;}
.noway a{ color: #F60;border-bottom:1px dotted #F60;}
.noway a:hover{color:#F60;border-bottom:1px solid #F60; }
.buy{background: url(shortcode/shortcode/buy.png) no-repeat scroll 15px 15px #EFF4D4; border: 1px solid #8FAD3D;color: #779B39;}
.buy a{color: #2b2b2b;border-bottom:1px dotted #2b2b2b;}
.buy a:hover{color:#2b2b2b;border-bottom:1px solid #2b2b2b; }
.task{background: url(shortcode/shortcode/clipboard.png) no-repeat scroll 15px 15px #E7E7E7; border: 1px solid #aaa;color: #333;}
.task a{ color: #4B7126;border-bottom:1px dotted #4B7126;}
.task a:hover{color:#4B7126;border-bottom:1px solid #4B7126; }
.shortcodestyle,.warning,.noway,.buy,.task p{text-indent: 0;}
.but_down { margin:10px 0}
.but_down a{float:left;display:block;background:transparent url(shortcode/shortcode/dlbutton.png) no-repeat left top;border:none;padding:0 0 0 60px;height:33px;font-family: 'Century Gothic',"Microsoft YaHei", Trebuchet MS, Arial, Tahoma, sans-serif;color:#046BB0;text-decoration:none;text-shadow:0 1px 1px #fff;}
.but_down a span{float:left;display:block;background:transparent url(shortcode/shortcode/dlbutton.png) no-repeat right top;padding:0 10px 0 0;line-height:33px;cursor:pointer;}
.but_down a:hover{color:#E23A0A;text-shadow:0 1px 1px #fff;background:transparent url(shortcode/shortcode/dlbutton.png) no-repeat left top;border-radius:0px;-webkit-border-radius:0px;-moz-border-radius:0px;-khtml-border-radius:0px;}

3、开始使用代码,格式如下,直接在编辑器输入即可!

灰色项目面板: [task]文字内容[/task]
红色禁止面板: [noway]文字内容[/noway]
黄色警告面板: [warning]文字内容[/warning]
绿色购买面板: [buy]文字内容[/buy]
下载样式 [Downlink href="http://www.xxx.com/xxx.zip"]download xxx.zip[/Downlink]
通用音乐播放器
默认不自动播放: [music]http://www.xxx.com/xxx.mp3[/music]
自动播放: [music auto=1]http://www.xxx.com/xxx.mp3[/music]
Mp3专用播放器
默认不循环不自动播放: [mp3]http://www.xxx.com/xxx.mp3[/mp3]
自动播放: [mp3 auto="1"]http://www.xxx.com/xxx.mp3[/mp3]
循环播放: [mp3 replay="1"]http://www.xxx.com/xxx.mp3[/mp3]
自动及循环播放: [mp3 auto="1" replay="1"]http://www.xxx.com/xxx.mp3[/mp3]
Flv专用播放器
默认不自动播放: [flv]http://www.xxx.com/xxx.flv[/flv]
自动播放: [flv auto="1"]http://www.xxx.com/xxx.flv[/flv]
注意:如果要使用这个播放器,一定要添加flv格式的视频文件。

4、将短代码写进后台编辑器,每个主题都不一样,具体待更新,因为我也不太懂!代码格式如下:

QTags.addButton( 'task', '灰色项目面板', "[task]", "[/task]");