为评论添加@reply功能

WordPress评论3.9K阅读模式

一、在评论页comments.php添加如下JS代码:

  1. <script language="javascript">
  2. //<![CDATA[
  3. function to_reply(commentID,author) {
  4. var nNd='@'+author+':';
  5. var myField;
  6. if (document.getElementById('comment') && document.getElementById('comment').type == 'textarea') {
  7. myField = document.getElementById('comment');
  8. } else {
  9. return false;
  10. }
  11. if (document.selection) {
  12. myField.focus();
  13. sel = document.selection.createRange();
  14. sel.text = nNd;
  15. myField.focus();
  16. }
  17. else if (myField.selectionStart || myField.selectionStart == '0') {
  18. var startPos = myField.selectionStart;
  19. var endPos = myField.selectionEnd;
  20. var cursorPos = endPos;
  21. myField.value = myField.value.substring(0, startPos)
  22. + nNd
  23. + myField.value.substring(endPos, myField.value.length);
  24. cursorPos += nNd.length;
  25. myField.focus();
  26. myField.selectionStart = cursorPos;
  27. myField.selectionEnd = cursorPos;
  28. }
  29. else {
  30. myField.value += nNd;
  31. myField.focus();
  32. }
  33. }
  34. //]]>
  35. </script>

 

二、在functions.php中加入如下代码:

 

  1. function to_reply() {
  2. ?>
  3. <a onclick='to_reply("<?php comment_ID() ?>", "<?php comment_author();?>")' href="#respond" style="cursor:pointer;"/>[@reply]</a>
  4. <?php
  5. }

 

三、在评论页后边添加”回复按钮”

 

  1. <?php to_reply(); ?>

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证