Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; comment_reply_notification has a deprecated constructor in /home/wwwroot/hector/www.yiwuye.com/wp-content/plugins/comment-reply-notification/comment-reply-notification.php on line 13
[wp技巧]Trackback和Pingback的分别调用和显示数目 « 一梧叶

[wp技巧]Trackback和Pingback的分别调用和显示数目

 - by Hector

如果你不了解Trackback和Pingback,请参考这里:Trackback、Pingback、Ping、XML-PRC ping区别

添加调用

如果你想自己加上Trackback的地址,那么在模板里加上下面的语句就可以了:

Trackback URL: <?php trackback_url(); ?>

wordpress自动开启了pingback功能,如果禁用了,那么在模板里加上下面的语句就可以了:

<link rel="pingback" href="…" />

当你发布完你的文章后,可以确认一下你的Trackback和Pingback是不是成功了。你可以选择编辑这篇文章,然后再下面的“Custom Fields”的上方,会有信息告诉你“Already Pinged …”和Trackback的地址。

显示评论数

显示总数
显示评论总数用comments_number函数,比如:

<!--?php comments_number(__('No Comments'), __('1 Comment'), __('% Comments')); ?-->
分别显示评论/Trackback/Pingback的数目
在functions.php中加入如下代码(在3.X和2.X中测试通过):

<!--?php /**  * 显示Trackback, pingback, comment, pings的数目,使用:  * fb_comment_type_count('ping');  * fb_comment_type_count('comment');  */  if ( !function_exists('fb_comment_type_count') ) { 	function fb_get_comment_type_count( $type='all', $zero = false, $one = false, $more = false, $post_id = 0) { 		global $cjd_comment_count_cache, $id, $post; 		if ( !$post_id ) 			$post_id = $post--->ID;
		if ( !$post_id )
			return;
		if ( !isset($cjd_comment_count_cache[$post_id]) ) {
			$p = get_post($post_id);
			$p = array($p);
			update_comment_type_cache($p);
		}
		;
		if ( $type == 'pingback' || $type == 'trackback' || $type == 'comment' )
			$count = $cjd_comment_count_cache[$post_id][$type];
		elseif ( $type == 'pings' )
			$count = $cjd_comment_count_cache[$post_id]['pingback'] + $cjd_comment_count_cache[$post_id]['trackback'];
		else
			$count = array_sum((array) $cjd_comment_count_cache[$post_id]);
		return apply_filters('fb_get_comment_type_count', $count);
	}
	// comment, trackback, pingback, pings, all
	function fb_comment_type_count( $type='all', $zero = false, $one = false, $more = false, $post_id = 0 ) {
		$number = fb_get_comment_type_count( $type, $zero, $one, $more, $post_id );
		if ( $number > 1 )
			$output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
		elseif ( $number == 0 )
			$output = ( false === $zero ) ? __('No Comments') : $zero;
		else // must be one
			$output = ( false === $one ) ? __('1 Comment') : $one;
		echo apply_filters('fb_comment_type_count', $output, $number);
	}
}
if ( !function_exists('fb_update_comment_type_cache') ) {
	function fb_update_comment_type_cache(&$queried_posts) {
		global $cjd_comment_count_cache, $wpdb;
		if ( !$queried_posts )
			return $queried_posts;
		foreach ( (array) $queried_posts as $post )
			if ( !isset($cjd_comment_count_cache[$post->ID]) )
				$post_id_list[] = $post->ID;
		if ( $post_id_list ) {
			$post_id_list = implode(',', $post_id_list);
			foreach ( array('', 'pingback', 'trackback') as $type ) {
				$counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount
							FROM $wpdb->posts
							LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID AND comment_approved = '1' AND comment_type='$type' )
							WHERE post_status = 'publish' AND ID IN ($post_id_list)
							GROUP BY ID");
				if ( $counts ) {
					if ( '' == $type )
						$type = 'comment';
					foreach ( $counts as $count )
						$cjd_comment_count_cache[$count->ID][$type] = $count->ccount;
				}
			}
		}
		return $queried_posts;
	}
	add_filter('the_posts', 'fb_update_comment_type_cache');
}?>

显示评论

下面是分类调用的代码,请参考
<!--?php if ( function_exists('wp_list_comments') ) { 	// WP 2.7 comment loop 	if ( have_comments() ) { ?-->
		<!--?php if ( empty($comments_by_type['comment']) ) { ?-->
<h2 id="comments"><!--?php fb_comment_type_count( 'comment' ); ?--></h2>
<ol class="commentlist">
				<!--?php wp_list_comments( 'type=comment' ); ?--></ol>
		<!--?php } ?-->
		<!--?php 		if ( function_exists( 'fb_comment_type_count' ) ) { 			// alternative type pings for trackback + pingback 			if ( empty($comments_by_type['$post_id']) ) { ?-->
<h2 id="pingback"><!--?php fb_comment_type_count( 'pingback', 'No Pingback', 'OnePingback', '% Pingbacks' ); ?--></h2>
<ol class="pingbacklist">
					<!--?php wp_list_comments('type=pingback'); ?--></ol>
			<!--?php } ?-->
			<!--?php 			// alternative type pings for trackback + pingback 			if ( empty($comments_by_type['trackback']) ) { ?-->
<h2 id="trackback"><!--?php fb_comment_type_count( 'trackback', 'No Trackback', 'One Trackback', '% Trackbacks' ); ?--></h2>
<ol class="trackbacklist">
					<!--?php wp_list_comments('type=trackback'); ?--></ol>
			<!--?php } 		} ?-->
<div class="navigation nav_comments">
<div class="alignleft"><!--?php previous_comments_link() ?--></div>
<div class="alignright"><!--?php next_comments_link() ?--></div>
</div>
	<!--?php } else { 		// this is displayed if there are no comments so far 	} }?-->

Leave a comment