Comments Enabled? Hack
-
I wanted to hide my comments links when comments were disabled on a post, so wrote a quick (well, when you decide you should learn PHP to do the hack, it takes a little longer) hack that can be used as a substitute for
comments_popup_link()
.smart_comments_popup_link()
, which takes the same arguments ascomments_popup_link()
, only displays the link if comments are open or at least one comment has already been posted (and moderated, if that option is set). It differs from just setting the last argument ofcomments_popup_link()
to ” because it depends only on comments, not on trackbacks or pingbacks. There’s probably some room for improvement in this regard. Any advice for improvement (or actual improvements) will be appreciated, since I’m trying to learn.
Add the following to the my-hacks.php file:
function smart_comments_popup_link($zero='No Comments', $one='1 Comment', $more='% Comments', $CSSclass='', $none='Comments Off') {
global $wpdb, $tableposts, $tablecomments, $id;
$comments_enabled = $wpdb->get_results("SELECT comment_status FROM $tableposts WHERE id=$id");
$comments_exist = $wpdb->get_var("SELECT COUNT(*) FROM $tablecomments WHERE comment_post_ID = $id AND comment_approved = '1'") > 0;
if (($comments_enabled[0]->comment_status == 'open') || $comments_exist) {
comments_popup_link($zero, $one, $more, $CSSclass, $none);
}
}
- The topic ‘Comments Enabled? Hack’ is closed to new replies.