WordPress 文章喜欢按钮

在一个国外主题上看到了,然后喜欢的不得了,于是自己动手各种改进,各种调试后成功了。

Javascript 代码,因为提交方式是采用了jQuery的ajax方法,所以前提是引入jQuery库。

<code>

<script type="text/javascript">
jQuery(document).ready(function() {
        jQuery(".likes").find("a").click(function(){
                heart = jQuery(this);
                // 获取文章的ID
                        post_id = heart.data("post_id");
                // Ajax 返回
                        jQuery.ajax({
                                type: "post",
                                url: ajax_var.url,
                                data: "action=post-like&nonce="+ajax_var.nonce+"&post_like=1&post_id="+post_id,
                                success: function(count){
                                        // 如果投票成功
                                        heart.find(".likenr").html(count);
                                }
                        });
                return false;
        });
})      
</script></code>

主要函数
将以下代码放到主题functions.php 中,在一些地方我做出了注释

<code>
function setup_yefengs_like( $post_id ){
    if(!is_numeric($post_id)) return;
    add_post_meta($post_id, 'post_like', '0', true);
}
add_action('publish_post', array(&$this, 'setup_yefengs_like'));

add_action('wp_ajax_nopriv_post-like', 'post_like');
add_action('wp_ajax_post-like', 'post_like');

function likescript() {
        if (!is_admin()) {
//这里是在创建绝对路径,异步提交方式是用了wordpress的admin-ajax.php来实现的。
        wp_localize_script('like_post', 'ajax_var', array(
                'url' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('ajax-nonce')
        ));
}
add_action('wp_enqueue_scripts', 'likescript');
//添加ajax动作


//设定 重复投票延时单位分钟 ,若设为0表示无限制
$timebeforerevote = 120; // minutes  

function post_like()
{
        //  安全性检查
        $nonce = $_POST['nonce'];
        if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
        die ( '错误!');

        if(isset($_POST['post_like'])){
                //  获取IP
                $ip = $_SERVER['REMOTE_ADDR'];
                $post_id = $_POST['post_id'];

                // 获取(从Meta)当前文章的浏览过的IP
                $meta_IP = get_post_meta($post_id, "voted_IP");
                $voted_IP = $meta_IP[0];

                if(!is_array($voted_IP))
                        $voted_IP = array();

                // 获取当前文章的浏览数
                $meta_count = 0;
                $meta_count = get_post_meta($post_id, "votes_count", true);

                // 检查是否重复投票,或不是,则投票数+1
                if(!hasAlreadyVoted($post_id)){
                        $voted_IP[$ip] = time();
                        // 保存和及更新投票数;
                        update_post_meta($post_id, "voted_IP", $voted_IP);
                        update_post_meta($post_id, "votes_count", ++$meta_count);
                        //输出 投票数 其实是ajax的返回值。
                        echo $meta_count;
                }
                else
                        echo "<div class='already'>你已经赞过</div>";
        }
        exit;
}

function hasAlreadyVoted($post_id)
{
        global $timebeforerevote;

        // 获取投票过的IP $meta_IP是一个数组
        $meta_IP = get_post_meta($post_id, "voted_IP");
        $voted_IP = $meta_IP[0];

        if(!is_array($voted_IP))
                $voted_IP = array();

        // 获取游客IP
        $ip = $_SERVER['REMOTE_ADDR'];

        // 判断是不是重复投票
        if(in_array($ip, array_keys($voted_IP))){
                $time = $voted_IP[$ip];
                $now = time();
                // 比较投票数的时间与投票时间
                if(round(($now - $time) / 60) > $timebeforerevote)
                        return false;
                return true;
        }
        return false;
}

function getPostLikeLink($post_id){
        $vote_count = get_post_meta($post_id, "votes_count", true);
        if(($vote_count=="")) $vote_count = 0;
        $output = '<div class="likes"> <a href="#" rel="external nofollow" class="externallink" class="like-count" data-post_id="'.$post_id.'"><span></span><div class="likenr">'.$vote_count.'</div></a> </div>';
        return $output;
}</code>

最后就是调用了,在文章的loop的适当位置增添以下代码即可

<code>
<?php echo getPostLikeLink(get_the_ID()) ?></code>

0 thoughts on “WordPress 文章喜欢按钮
添加一条新回复 回到顶部

亲爱的,主人已经关闭了这篇文章的评论 。