温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用PHP怎么在WordPress中实现一个留言楼层号功能

发布时间:2020-12-17 14:24:25 来源:亿速云 阅读:172 作者:Leah 栏目:开发技术

利用PHP怎么在WordPress中实现一个留言楼层号功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

在主题文件 functions.php中找到$GLOBALS['comment'] = $comment;在后面加上下面的代码:

/* 主评论计数器 */
 global $commentcount,$wpdb, $post;
 if(!$commentcount) { //初始化楼层计数器
  if ( get_option('comment_order') === 'desc' ) { //倒序
  $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent");
  $cnt = count($comments);//获取主评论总数量
  $page = get_query_var('cpage');//获取当前评论列表页码
  $cpp=get_option('comments_per_page');//获取每页评论显示数量
  if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
   $commentcount = $cnt + 1;//如果评论只有1页或者是最后一页,初始值为主评论总数
  } else {
   $commentcount = $cpp * $page + 1;
  }
  }else{ //顺序
  $page = get_query_var('cpage')-1;
  $cpp=get_option('comments_per_page');//获取每页评论数
  $commentcount = $cpp * $page;
  }
 }
/* 主评论计数器 end */
 if ( !$parent_id = $comment->comment_parent ) {
  $commentcountText = '<div class="floor">';
  if ( get_option('comment_order') === 'desc' ) { //倒序
  $commentcountText .= --$commentcount . '楼';
  } else {
  switch ($commentcount) {
   case 0:
   $commentcountText .= '<span>沙发!</span>'; ++$commentcount;
   break;
   case 1:
   $commentcountText .= '<span>板凳!</span>'; ++$commentcount;
   break;
   case 2:
   $commentcountText .= '<span>地板!</span>'; ++$commentcount;
   break;
   default:
   $commentcountText .= ++$commentcount . '楼';
   break;
  }
  }
  $commentcountText .= '</div">';
 }
 }

然后在合适的位置加上以下代码输出楼层号

<?php echo $commentcountText; //主评论楼层号 - by zwwooooo ?>

修改之后的代码应该是这样的(以官方最新的 wp_list_comments() 回调函数代码为例):

<?php
function mytheme_comment($comment, $args, $depth) {
 $GLOBALS['comment'] = $comment;
 /* 主评论计数器 by zwwooooo Modified Gimhoy(http://blog.gimhoy.com) */
 global $commentcount,$wpdb, $post;
 if(!$commentcount) { //初始化楼层计数器
  if ( get_option('comment_order') === 'desc' ) { //倒序
  $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = $post->ID AND comment_type = '' AND comment_approved = '1' AND !comment_parent");
  $cnt = count($comments);//获取主评论总数量
  $page = get_query_var('cpage');//获取当前评论列表页码
  $cpp=get_option('comments_per_page');//获取每页评论显示数量
  if (ceil($cnt / $cpp) == 1 || ($page > 1 && $page == ceil($cnt / $cpp))) {
   $commentcount = $cnt + 1;//如果评论只有1页或者是最后一页,初始值为主评论总数
  } else {
   $commentcount = $cpp * $page + 1;
  }
  }else{ //顺序
  $page = get_query_var('cpage')-1;
  $cpp=get_option('comments_per_page');//获取每页评论数
  $commentcount = $cpp * $page;
  }
 }
 /* 主评论计数器 end */
 if ( !$parent_id = $comment->comment_parent ) {
  $commentcountText = '<div class="floor">';
  if ( get_option('comment_order') === 'desc' ) { //倒序
  $commentcountText .= --$commentcount . '楼';
  } else {
  switch ($commentcount) {
   case 0:
   $commentcountText .= '<span>沙发!</span>'; ++$commentcount;
   break;
   case 1:
   $commentcountText .= '<span>板凳!</span>'; ++$commentcount;
   break;
   case 2:
   $commentcountText .= '<span>地板!</span>'; ++$commentcount;
   break;
   default:
   $commentcountText .= ++$commentcount . '楼';
   break;
  }
  }
  $commentcountText .= '</div">';
 }
 }

 extract($args, EXTR_SKIP);

 if ( 'div' == $args['style'] ) {
 $tag = 'div';
 $add_below = 'comment';
 } else {
 $tag = 'li';
 $add_below = 'div-comment';
 }
?>
 <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
 <?php if ( 'div' != $args['style'] ) : ?>
 <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
 <?php endif; ?>
 <div class="comment-author vcard">
 <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
 <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
 </div>
<?php if ($comment->comment_approved == '0') : ?>
 <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
 <br />
<?php endif; ?>

 <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
 <?php
  /* translators: 1: date, 2: time */
  printf( __('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),' ','' );
 ?>
 </div>

 <?php comment_text() ?>

 <div class="reply">
 <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
 </div>

 <?php echo $commentcountText; //主评论楼层号 - by zwwooooo ?>

 <?php if ( 'div' != $args['style'] ) : ?>
 </div>
 <?php endif; ?>
<?php
 }

看完上述内容,你们掌握利用PHP怎么在WordPress中实现一个留言楼层号功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI