好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

WordPress评论列表中获取楼层或序号 - WordPress

WordPress评论列表中获取楼层或序号

文章来给大家介绍在WordPress评论列表中获取楼层或序号实现方法,我们会看到很多使用wordpress博客的朋友评论可以有楼层号的,下面是我自己的实现方法各位可参考.

我们的评论列表中,偶尔我们希望获得这个评论的序号,如果能够按照一定的规律获得序号,那么就可以为特定的评论添加一些新信息,例如为评论添加楼层,或者在第5条评论后添加一段广告,但可惜的是,wordpress本身是没有提供这个功能的,我们无法在mytheme_comment中拥有一个全局参数来获取每篇文章的评论序号,对于如何构建自己的评论列表如何自己设计wordpress评论列表及评论框一文说的非常干净,如果你已经对构建问题非常清楚,应该就会发现本文所提出来的问题。

我们来实现本文要实现的具体目标吧。

序号机制↑

在wordpress中没有提供每篇文章独立的序号,而我们不能简单的使用comment_ID来代替这个序号,这样根本不能反映出这篇文章中该评论的位置,因此,我们只能使用php来实现,我所使用的是$GLOBAL全局参数设置方法,代码如下:

if (! $GLOBALS [ 'current_comment_order' ]){       $GLOBALS [ 'current_comment_order' ] = 1;  } else {       $GLOBALS [ 'current_comment_order' ] ++;       if ( $GLOBALS [ 'current_comment_order' ] >  $args [ 'per_page' ]) $GLOBALS [ 'current_comment_order' ] = 0;  } 

如上代码,如果不存在$GLOBALS['current_comment_order']就将它设置为1,如果已经有了,就在原有的基础上加1,如果到达最大序号时,就命令它为0,至于为何要使用$GLOBAL,毋庸置疑,就是因为wordpress没有一个全局参数来得到序号.

如何使用 ↑

和上面提到的那篇文章一样,你要设计自己的评论列表,一个是要处理comments.php文件,另外还要在functions.php中增加一个用来呈现列表的函数,这个函数将直接被wp_list_comments调用而无需增加钩子,我们姑且将这个函数称为mytheme_comment($comment,$arg,$depth),它不仅不需要加入钩子,而且不需要关闭列表标签,例如你打算用<li>来呈现列表,你不要写</li>,而是让<li>敞开,因为你要知道,你可能是嵌套显示你的评论,wordpress会自动为你关闭它。

实例代码如下:

function  mytheme_comment( $comment , $args , $depth ){       $comment_id  =  $comment ->comment_ID;       $comment_author  =  $comment ->comment_author;       $comment_parent  =  $comment ->comment_parent;       $comment_post  =  $comment ->comment_post_ID;  ?>  <li <?php comment_class( $replytocom . $current ); ?>>      <div id= "comment-<?php comment_ID() ?>" >          <div><?php  echo  get_avatar( $comment , $size = '40' ); ?></div>          <div>              <span><?php  echo  get_comment_author_link(); ?></span>              <span>#<?php comment_ID(); ?>楼</span>              <?php  if ( $comment_parent ) echo   '<span>回复给<a href="#comment-' . $comment_parent . '" rel="nofollow">@' . $comment_parent . '楼</a></span>' ; ?>              <span><?php  echo  get_comment_date( 'Y/m/d ' ); ?></span>              <span><?php  echo  get_comment_time( 'H:i:s' ); ?></span>              <span><a href= "<?php echo get_permalink($comment_post); ?>&replytocom=<?php comment_ID(); ?>#respond"  rel= "nofollow"  data-comment-id= "<?php comment_ID(); ?>"  data-comment-author= "<?php echo $comment_author; ?>" >回复</a></span>              <span><?php edit_comment_link( '编辑' , '' , '' ) ?></span>          </div>          <div>          <?php comment_text(); ?>          <?php  if  ( $comment ->comment_approved ==  '0' )printf( '<div>%s</div>' , '您的见解正在审核中,很快就会出现在评论列表~~' ); ?>          <div></div>      </div>      <?php  } 

至于其中的三个参数,你只需要具体了解wp_list_comments函数即可,总之它们都很有用。

将深度和楼层结合起来 ↑

有这么一种想法,你只需要将嵌套的评论列表中的第一层作为计算对象,每个嵌套算作一楼,这样一来,你可以在第一楼后面加上一个短短的广告代码,我们通过下面的代码来实现:

function  mytheme_comment( $comment , $args , $depth ){       if ( $depth  == 1) :  // 如果你需要对1、2层都计算的话,可以试试($depth == 1 || $depth == 2)            if (! $GLOBALS [ 'current_comment_order' ]){               $GLOBALS [ 'current_comment_order' ] = 1;          } else {               $GLOBALS [ 'current_comment_order' ] ++;               if ( $GLOBALS [ 'current_comment_order' ] >  $args [ 'per_page' ]) $GLOBALS [ 'current_comment_order' ] = 0;          }           if ( $GLOBALS [ 'current_comment_order' ] == 1) :  // 这里表示第一楼,如果要表示第二楼,把全等号后面改为2即可           ?>你的广告代码<?php           endif ;       endif ;  } 

翻页的情况怎么考虑 ↑

评论多了以后翻页是在所难免的,幸运的是,wordpress已经提供了翻页所需要的一些材料,当前页码,总的页码,每页多少条都可以通过一种途径获取:

function  mytheme_comment( $comment , $args , $depth ){       $current_page  =  $args [ 'page' ];       $totle_page  = get_comment_pages_count();       $per_page_num  =  $args [ 'per_page' ];       $totle_num  = get_comments_number();  } 

翻页的时候,你首先要考虑的是,你打算只在第一页或第几页显示某个代码,用下面的判断:

if($current_page == 2) : // 如果当前是第二页评论时,一般会在URL中反映为comment-page-2而如果你要计算一个总体的位置,比如说你想在第40条显示某段代码,你就可以通过每页的条数和当前页数结合起来进行判断,例如如下代码:

if($per_page_num*($current_page-1) + $GLOBAL['current_comment_order'] == 40) :

当然,你可能需要的是$GLOBAL['current_comment_order']为40时,也就是如下代码:

if($GLOBAL['current_comment_order'] == 40) :

不过,这个还真不能实现,因为本身$GLOBAL['current_comment_order']就是记录当前页面评论条数的序号的,所以老老实实用上一个判断吧。

总之,添加楼层我们使用到了$GLOBAL['current_comment_order'],核心代码是第一段代码,之后你可以结合文章开头介绍的那篇文章做出更强大的调用功能.

查看更多关于WordPress评论列表中获取楼层或序号 - WordPress的详细内容...

  阅读:57次