WordPress.org

Ready to get started?Download WordPress

Forums

Comments Enabled? Hack (4 posts)

  1. Anonymous
    Unregistered
    Posted 10 years ago #

    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 as comments_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 of comments_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);
    }
    }

  2. Anonymous
    Unregistered
    Posted 10 years ago #

    Incidentally, if anyone knows a better way of showing that code, please advise. I was going to put it up at the wiki, but I can't seem to log into it to modify it.

  3. eric
    Member
    Posted 10 years ago #

    Most likely your problem with the wiki is because your user name is too short, it happened to others with short names and me as well even with 4 letters. Dunno why, but making up a longer user name seems to fix it.

  4. pao
    Member
    Posted 10 years ago #

    Now pluginized. See the wiki link in the post immediately above.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.