wordpress实现文章点赞功能

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

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

  1. add_action('wp_ajax_nopriv_favorite_praise', 'favorite_praise');
  2. add_action('wp_ajax_favorite_praise', 'favorite_praise');
  3. function favorite_praise(){
  4. global $wpdb,$post;
  5. $id = $_POST["um_id"];
  6. $action = $_POST["um_action"];
  7. if ( $action == 'ding'){
  8. $specs_raters = get_post_meta($id,'favorite_praise',true);
  9. $expire = time() + 99999999;
  10. $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
  11. setcookie('favorite_praise_'.$id,$id,$expire,'/',$domain,false);
  12. if (!$specs_raters || !is_numeric($specs_raters)) {
  13. update_post_meta($id, 'favorite_praise', 1);
  14. }
  15. else {
  16. update_post_meta($id, 'favorite_praise', ($specs_raters + 1));
  17. }
  18. echo get_post_meta($id,'favorite_praise',true);
  19. }
  20. die;
  21. }

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

  1. $.fn.postLike = function() {
  2. if ($(this).hasClass('done')) {
  3. alert('您已赞过本博客');
  4. return false;
  5. } else {
  6. $(this).addClass('done');
  7. var id = $(this).data("id"),
  8. action = $(this).data('action'),
  9. rateHolder = $(this).children('.count');
  10. var ajax_data = {
  11. action: "favorite_praise",
  12. um_id: id,
  13. um_action: action
  14. };
  15. $.post("/wp-admin/admin-ajax.php", ajax_data,
  16. function(data) {
  17. $(rateHolder).html(data);
  18. });
  19. return false;
  20. }
  21. };
  22. $(document).on("click", ".favoritePraise",
  23. function() {
  24. $(this).postLike();
  25. });
  26.  
  27. });

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

  1. <div class="post-like">
  2.     <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">
  3.         <?php if( get_post_meta($post->ID,'favorite_praise',true) ){
  4.             echo get_post_meta($post->ID,'favorite_praise',true);
  5.                 } else {
  6. echo '0';
  7. }?></span>
  8.     </a>
  9. </div>

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

  1. .post-like{text-align:center;padding:10px}
  2. .post-like a{ background-color:#21759B;border-radius: 3px;color: #FFFFFF;font-size: 12px;padding: 5px 10px;text-decoration: none;outline:none}
  3. .post-like a.done, .post-like a:hover{background-color:#eee;color:#21759B;}
  4. .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   

表情

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