在wordpress主题评论中添加@reply功能

@reply回复功能,没必要再添加一个插件来做。我们可以通过修改当前所调用的wordpress主题来实现该功能。

方法如下:

一、在评论页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. }

三、在评论页<?php comment_author_link() ?>后边添加”回复按钮”

<?php to_reply(); ?>
崔凯博客
Powered by WordPress | Theme by ck web design | TOP 顶部
Copyright © 崔凯 All rights reserved. 蜀ICP备12025891号.