wordpress实现文章点赞功能

  • 2019-01-19 19:58:48
  • 8,442 次阅读
  • 稿源:天马行空
最近,想在自己改版的网站上增加一个点赞功能,以此来丰富网站的内容。于是在网上查询该功能如何实现,一开始找了一个源码,在本地测试,不管怎么弄,点赞的按钮就是不起作用。我想程序哪里是不是出现了问题,查看了好多博客都是发表的相同的代码程序,既然人家用的好好的,为啥我的不起作用呢?经过一番折腾,弄了好久还是不行,我想第二天放到服务器空间里起作用吗,结果真的管用了。给大家分享一下代码,如下:
favorite_praise

第一步:将下面的代码放到主题的functions里面

add_action('wp_ajax_nopriv_favorite_praise', 'favorite_praise');
add_action('wp_ajax_favorite_praise', 'favorite_praise');
function favorite_praise(){
    global $wpdb,$post;
    $id = $_POST["um_id"];
    $action = $_POST["um_action"];
    if ( $action == 'ding'){
        $specs_raters = get_post_meta($id,'favorite_praise',true);
        $expire = time() + 99999999;
        $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
        setcookie('favorite_praise_'.$id,$id,$expire,'/',$domain,false);
        if (!$specs_raters || !is_numeric($specs_raters)) {
            update_post_meta($id, 'favorite_praise', 1);
        } 
        else {
            update_post_meta($id, 'favorite_praise', ($specs_raters + 1));
        }
        echo get_post_meta($id,'favorite_praise',true);
    } 
    die;
}

第二步:新建一个js文件,将代码放进去,在底部引用

$.fn.postLike = function() {
    if ($(this).hasClass('done')) {
        alert('您已赞过本博客');
        return false;
    } else {
        $(this).addClass('done');
        var id = $(this).data("id"),
        action = $(this).data('action'),
        rateHolder = $(this).children('.count');
        var ajax_data = {
            action: "favorite_praise",
            um_id: id,
            um_action: action
        };
        $.post("/wp-admin/admin-ajax.php", ajax_data,
        function(data) {
            $(rateHolder).html(data);
        });
        return false;
    }
};
$(document).on("click", ".favoritePraise",
    function() {
        $(this).postLike();
});

});

第三步:将按钮放到文章内容页合适的位置

<div class="post-like">
    <a href="javascript:;" data-action="ding" data-id="<?php the_ID(); ?>" class="favoritePraise <?php if(isset($_COOKIE['favorite_praise_'.$post->ID])) echo 'done';?>">喜欢 <span class="count">
        <?php if( get_post_meta($post->ID,'favorite_praise',true) ){
            echo get_post_meta($post->ID,'favorite_praise',true);
                } else {
echo '0';
}?></span>
    </a>
</div>

第四步:将样式放到css样式表里面

.post-like{text-align:center;padding:10px}
.post-like a{ background-color:#21759B;border-radius: 3px;color: #FFFFFF;font-size: 12px;padding: 5px 10px;text-decoration: none;outline:none}
.post-like a.done, .post-like a:hover{background-color:#eee;color:#21759B;}
.post-like a.done{cursor:not-allowed}
喜欢 82

文章评论 (4)

  1. Ishtar Company LLC说道:

    For most up-to-date information you have to go to see world wide web
    and on internet I found this web page as a best website
    for latest updates.

    [1楼]网友 Linux | Chrome 67.0.3396.87   
  2. WordPress说道:

    Nߋt onjly that sometimes new theme add neww functionality to yоuг website.

    Worɗ – Prеss Plugin is really a program that is put together by the plugin developers thɑt enable you to addd a huge selectіon of features, effects and benefits for your bloɡ.
    They are used to deliver certain functionalities for thе CMS which otherwise
    would ϲall for а development intensive activity.

    [2楼]网友 Linux | Firefox 52.9   
  3. sodium bicarbonate说道:

    I don’t even know the way I ended up right here,
    however I thought this put up used too bbe good.
    I don’t realize who you’re however certainly you are going
    to a famous blogger in thee event you aren’t already.
    Cheers!

    [3楼]网友 Windows NT | Chrome 63.0.3239.132   
  4. Lashay说道:

    Wow, marvelous blog layout! How long have you been running a blog for?
    you make running a blog glance easy. The full look of your website is magnificent, as smartly as the content!

    [4楼]网友 Mac OS X | Chrome 63.0.3239.108   

表情

大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头