贝塔罗拉

WordPress短代码应用实例

时间: 2010-02-02 / 分类: wordpress / 27个评论 发表评论

在阅读本文之前建议先对wordpress段代码做一些了解,可以参考帕兰的《精通wodpress短代码》.

1.插入Mp3音乐

本方法参考了木木的《给WordPress主题添加短代码功能》,但是我不喜欢豆瓣的那个播放器,所以去Blogcastone偷了下面我用的播放器过来-_-!!

下载播放器文件放到你的主题目录里面,下载地址请点击第二个实例中的下载图标,然后把以下代码加入到主题functions.php里面:

1
2
3
4
5
6
/**Mp3 player */
	function mp3player($atts, $content=null){
	extract(shortcode_atts(array("auto"=>'no',"loop"=>'no'),$atts));	
	return '<embed src="'.get_bloginfo(" template_url").'/player.swf?soundFile='.$content.'&bg=0xeeeeee&leftbg=0x357dce&lefticon=0xFFFFFF&rightbg=0xf06a51&rightbghover=0xaf2910&righticon=0xFFFFFF&righticonhover=0xffffff&text=0x666666&slider=0x666666&track=0xFFFFFF&border=0x666666&loader=0x9FFFB8&loop='.$loop.'&autostart='.$auto.'" type="application/x-shockwave-flash" wmode="transparent" allowscriptaccess="always" width="290" height="30">';
}
add_shortcode('mp3','mp3player');

默认关闭自动播放,关闭自动循环,调用时输入以下代码:

1
2
[mp3]Yoururl.mp3[/mp3]默认关闭自动播放和循环
[mp3 auto="yes" loop="no"]Yoururl.mp3[/mp3]开启自动播放、关闭循环,其他自己组合就是Yes和No控制

2.下载文件添加图标

给要下载的文件添加图标即美观又醒目,但是每次都要插入图片就繁琐了,利用wordpress短代码轻松解决它,添加如下代码:

1
2
3
4
5
6
7
8
 /** Download Image */
  function jomor_download($atts, $content = null)
    {
 
     return '<a href="'.$content.'" target="_blank"><img src="http://jomor.org/download/download.gif" border="0" /></a>';
    }
 
    add_shortcode("download", "jomor_download");

调用方法:

1
[download]Yoururl[/download]

3.插入Google Adsense广告

用wordpres短代码插入Google Adsense太方便了,插入以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function adsense_shortcode( $atts ) {
	extract(shortcode_atts(array(
	    'format' => '1',
	), $atts));
 
	switch ($format) {
		case 1 :
			$ad = '<script type="text/javascript"><!--
			google_ad_client = "pub-6928386133078955";
			/* 234x60, created 16/09/08 */
			google_ad_slot = "0834408702";
			google_ad_width = 234;
			google_ad_height = 60;
			//-->
			</script>
			<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
			</script>';
		break;
	}
	return $ad;
}
add_shortcode('adsense', 'adsense_shortcode');

调用方法:

1
[adsense]

自己给format增加case,从而管理多个广告,这个大家都比较讨厌广告,我就不提供演示了。

4.插入相关文章

这个功能可以利用相关插件实现,也可以利用wordpress短代码实现,看个人喜好了,插入以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function related_posts_shortcode( $atts ) {
	extract(shortcode_atts(array(
	    'limit' => '5',
	), $atts));
 
	global $wpdb, $post, $table_prefix;
 
	if ($post->ID) {
		$retval = '<ul>';
 		// Get tags
		$tags = wp_get_post_tags($post->ID);
		$tagsarray = array();
		foreach ($tags as $tag) {
			$tagsarray[] = $tag->term_id;
		}
		$tagslist = implode(',', $tagsarray);
 
		// Do the query
		$q = "SELECT p.*, count(tr.object_id) as count
			FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id  = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
				AND p.post_status = 'publish'
				AND p.post_date_gmt < NOW()
 			GROUP BY tr.object_id
			ORDER BY count DESC, p.post_date_gmt DESC
			LIMIT $limit;";
 
		$related = $wpdb->get_results($q);
 		if ( $related ) {
			foreach($related as $r) {
				$retval .= '<li><a title="'.wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'</a></li>';
			}
		} else {
			$retval .= '
	<li>No related posts found</li>';
		}
		$retval .= '</ul>';
		return $retval;
	}
	return;
}
add_shortcode('related_posts', 'related_posts_shortcode');

调用方法:

1
[related_posts limit="x"]

wordpress短代码学习起来并不难,更多的短代码可以参考一下这篇文章《10 incredibly cool WordPress shortcodes》

本文链接: http://www.betarola.com/articles/wordpress-shortcodes.html 转载请注明出处!