给朋友圈加个点赞功能!

Published in May-19-2023


文章目录

之前把说说页面改成了朋友圈样式,感觉确实挺不错的,看着特别有感觉,有小伙伴建议加个评论功能,所以便捣鼓一下评论和点赞功能。由于技术有限。尚没有克服点击评论会自动跳转到说说详情页的问题。

曹同学

所以便只加了朋友圈点赞功能~说说太多了,一页有点多,顺便做个分页~

functions.php

/*点赞功能*/
add_action('wp_ajax_nopriv_bigfa_like', 'bigfa_like');
add_action('wp_ajax_bigfa_like', 'bigfa_like');
function bigfa_like(){
    global $wpdb,$post;
    $id = $_POST["um_id"];
    $action = $_POST["um_action"];
    if ( $action == 'ding'){
    $bigfa_raters = get_post_meta($id,'bigfa_ding',true);
    $expire = time() + 99999999;
    $domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
    setcookie('bigfa_ding_'.$id,$id,$expire,'/',$domain,false);
    if (!$bigfa_raters || !is_numeric($bigfa_raters)) {
        update_post_meta($id, 'bigfa_ding', 1);
    } 
    else {
            update_post_meta($id, 'bigfa_ding', ($bigfa_raters + 1));
        }
    echo get_post_meta($id,'bigfa_ding',true);
    } 
    die;
}

JS代码

 <!--点赞功能js-->
<script type="text/javascript">
$.fn.postLike=function(){if($(this).hasClass('done')){return false}else{$(this).addClass('done');var id=$(this).data("id"),action=$(this).data('action'),rateHolder=$(this).children('.count');var ajax_data={action:"bigfa_like",um_id:id,um_action:action};$.post("/wp-admin/admin-ajax.php",ajax_data,function(data){$(rateHolder).html(data)});return false}};$(document).on("click",".favorite",function(){$(this).postLike()});
</script>

主体HTML代码

<!--点赞功能开始-->
<div class="post-like">
<a href="javascript:;" data-action="ding" data-id="<?php the_ID(); ?>" class="favorite<?php if(isset($_COOKIE['bigfa_ding_'.$post->ID])) echo ' done';?>"><i class="fa fa-heart" aria-hidden="true"></i> 喜欢[<span class="count"><?php if( get_post_meta($post->ID,'bigfa_ding',true)){ echo get_post_meta($post->ID,'bigfa_ding',true);} else { echo '0';}?></span>]
</a>
</div>
</div>
<!---->

每页数量

<?php $limit = get_option('posts_per_page');$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  
query_posts('post_type=shuoshuo&post_status=publish&showposts=' . $limit=10 . '&paged=' . $paged);if (have_posts()) : while (have_posts()) : the_post(); ?>

分页

<!--分页功能-->
<?php if (function_exists('wp_pagenavi')) wp_pagenavi();else { ?><span class="say-previous"><?php previous_posts_link('上一页') ?></span>  <span class="say-next"><?php next_posts_link('下一页') ?><?php } ?></span>

效果图

朋友圈 - 没有故事的曹同学.png



评论一条评论

  1. 勿忘初心
    7个月前 (09-24)

    你好,看你的站做的挺不错的,有没有出手的打算,想出手的话,联系QQ1587894193。

曹同学的bilibili 曹同学的知乎 曹同学的email
返回网站顶部
正在获取,请稍候...
00:00/00:00