在 WP 的 theme 中有个 comments.php 文件,是评论模板文件。但另一个名字相关的 comments-popup.php 文件,一直不知道它有什么用。甚至删除了它,也没见系统有什么反常的。
其实,它还是有用的,就是点击文章的“评论”,可以弹出该页面,在里边填写评论。但因为“弹出评论页面”的功能默认是被关闭的,所以很多人不了解它。要打开弹出评论页面的功能,编辑 header.php 文件,看到第 21 行(WP2.9.1版本):
<?php //comments_popup_script(); // off by default ?>
这里就是开关,把该句的注释去掉。然后刷新博客主页面,会看到代码的 head 部分多了 js 脚本:
<script type='text/javascript'>
function wpopen (macagna)
{
window.open(macagna, '_blank', 'width=400,height=400,scrollbars=yes,status=yes');
}
</script>
评论链接的脚本变为:
<div class="feedback">
<a href="http://.../?comments_popup=230" οnclick="wpopen(this.href); return false" title="...">评论(0)</a>
</div>
注意js脚本 οnclick="wpopen(this.href); return false" 写的很巧妙,在不改变链接结构的情况下,就加了 onclick 事件函数,就改变了链接的打开行为。而不是写为:
<a href="#" οnclick="wpopen('http://.../?comments_popup=230'); return false" title="...">评论(0)</a>
发现本技巧,参考了老外的一篇文章:
(地址:http://playworkplay.com/blog/understanding-comments-popupphp/)
Understanding comments-popup.php
28.01.2008
I have a confession to make.
Up until now, I didn’t have any idea how comments-popup.php works. Oh, it’s not like I haven’t done any research. The Codex’s Theme Development page, for instance, explains this file as:
The popup comments template. If not present, comments-popup.php from the “default” Theme is used.
And it doesn’t link to any specific page explaining more about it. On another page, Using Themes, the information is even more sparse:
Popup Comments Template = comments-popup.php
For quite a long time, my understanding is that this file is used if, somewhere within WordPress, the option to display comments as a popup is activated. When that magic switch is on, people who click the comment link will get a popup window with the comment area inside it. The question is, where on earth do I find that option?
Oh, don’t ask me how many hours I’ve spent looking around for it. I’m even tempted to enter the famous Konami Code inside the WP admin area just to see if a secret page will open and show me the magic “Display Comment as Popup” option—it’s really that hard to find!
And no wonder. I just got the revelation today, and it turns out there’s never been such option within the admin area. Nope. I was digging in the wrong place.
The option to use comments-popup.php, ladies and gentlemen, depends solely on the presence of a single PHP line inside a theme’s header file. Go open the header.php of the Classic theme (available in any default install of WordPress), line 21 you will see:
view sourceprint?1.<?php //comments_popup_script(); // off by default ?>Yes! That’s the magic switch. The Holy Grail of Popup Commenting. I uncommented that and, bam, the very first WordPress comment popup appared before my very eyes. Try it.
By searching for that PHP function into the Codex, I found this small but complete explanation about popup comments, which I wished I’ve found before.
Anyways, it’s also interesting to note that many, many themes out there have comments-popup.php inside them but no comments_popup_script() function, making it practically useless. It’s not even available within Kubrick, WordPress’s current default theme.
I’m not sure why comments_popup_script() function is not included within the default theme (not even in commented form like the one inside the classic theme); to me and many others, Kubrick is the first theme we tear apart and learn in the process. Perhaps the usage of popup window is discouraged?
I’m not sure. But in my case, I’m very likely to leave out the comments-popup.php like I often do. I used to do it out of the fact that I don’t know what it does, and because Sandbox theme—another good theme I learn a lot from—does not have it. But now, after some thinking, I’ll just leave it out for one more reason:
I’ve never ever visit a WP-powered blog with popup comments (have you?). I’m sure that’s what most users experience as well—we’re already accustomed to the open-new-page-for-comments behavior.
Better not mess with that.
作者:张庆(网眼) 2010-4-6
来自“网眼视界”:http://blog.why100000.com
“十万个为什么”电脑学习网:http://www.why100000.com