当前位置: 首页 > 资源分享 > 正文
avatar

看到很多网站评论都有赞、踩功能,你是不是也想要?wordpress不借助任何插件也可以实现哦。 我们以rela […]

看到很多网站评论都有赞、踩功能,你是不是也想要?wordpress不借助任何插件也可以实现哦。

我们以relax2.0.1主题(http://www.soyep.net/616.html)为例。

首先在主题的functions.php里面的<?php 后面写入如下代码:

//评论顶踩
add_action('wp_ajax_nopriv_do_comment_rate', 'do_comment_rate');
add_action('wp_ajax_do_comment_rate', 'do_comment_rate');
function do_comment_rate(){
    if (!isset($_POST["comment_id"]) || !isset($_POST["event"])) {
        $data = array("status"=>500,"data"=>'?');
        echo json_encode($data);
    } else {
        $comment_id = $_POST["comment_id"];
        $event = $_POST["event"];
        $expire = time() + 99999999;
        $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : 
        false; // make cookies work with localhost
        setcookie('comment_rated_'.$comment_id,$comment_id,$expire,'/',$domain,false);
        $_comment_up = get_comment_meta($comment_id,'_comment_up',true);
        $_comment_down = get_comment_meta($comment_id,'_comment_down',true);
        if ($event == "up") {
            if (!$_comment_up) {
                update_comment_meta($comment_id, '_comment_up', 1);
            } else {
                update_comment_meta($comment_id, '_comment_up', 
           ($_comment_up + 1));
            }
        } else {
            if (!$_comment_down || $_comment_down == '' || !is_numeric($_comment_down)) {
                update_comment_meta($comment_id, '_comment_down', 1);
            } else {
                update_comment_meta($comment_id, '_comment_down', ($_comment_down + 1));
            }
        }
        $data = array();
        $_comment_up = get_comment_meta($comment_id,'_comment_up',true);
        $_comment_down = get_comment_meta($comment_id,'_comment_down',true);
        $data = array("status"=>200,"data"=>array("event"=>$event,
        "_comment_up"=>$_comment_up,"_comment_down"=>$_comment_down));
        echo json_encode($data);
    }
    die;
}
function comment_rate($comment_ID = 0,$echo = true){
    $_comment_up = get_comment_meta($comment_ID,'_comment_up',true) ? 
    get_comment_meta($comment_ID,'_comment_up',true) : 0;
    $_comment_down = get_comment_meta($comment_ID,'_comment_down',true) ? 
    get_comment_meta($comment_ID,'_comment_down',true) : 0 ;
    $done = "";
    if (isset($_COOKIE['comment_rated_'.$comment_ID])) $done = " rated";
    $content = '<span style="float:right;" class="comment--like'.$done.'" 
    data-commentid="'.$comment_ID.'"><a href="javascript:;" data-event="up" title="赞">
    <i class="iconfont icon-arrowup"></i><button type="button" class="btn1" 
    onmouseover="this.style.backgroundPosition=&#039left -16px&#039" 
    onmouseout="this.style.backgroundPosition=&#039left top&#039" 
    style="background-position: 0% 0%;"></button><em class="action1">'.$_comment_up.'
    </em></a><a href="javascript:;" data-event="down" title="踩"><i class="iconfont 
    icon-arrowdown"></i><button type="button" class="btn2" onmouseover="this.style.
    backgroundPosition=&#039left -16px&#039" onmouseout="this.style.backgroundPosition=
    &#039left top&#039" style="background-position: 0% 0%;"></button><em class="action2">
    '.$_comment_down.'</em></a></span>';
    if ($echo) {
        echo $content;
    } else {
        return $content;
    }
}
add_action('delete_comment', 'delete_comment_ratings_fields');
function delete_comment_ratings_fields($comment_ID) {
    global $wpdb;
    delete_comment_meta($comment_ID, '_comment_up');
    delete_comment_meta($comment_ID, '_comment_down');
}
分页: 1 2 3下一页
分享:
---------WwW.13qing.com---------
猜你也喜欢的:

给WordPress添加评论投票(赞、踩)功能:等您坐沙发呢!

发表评论