wordpress网站点赞功能制作步骤:
- 下载wordpress网站点赞处理文件。然后在自己的wordpress模板的functions.php中粘贴以下函数:
require_once( TEMPLATEPATH . 'function-dianzan.php');
【5uyard】
也可以将function-dianzan.php中的代码直接放到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; } - 在wordpress底部模板(footer.php)中插入以下的JS调用代码。
<script type="text/javascript"> $(document).ready(function() { $.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: "bigfa_like", um_id: id, um_action: action }; $.post("<?php bloginfo('url');?>/wp-admin/admin-ajax.php", ajax_data, function(data) { $(rateHolder).html(data); }); return false; } }; $(document).on("click", ".favorite", function() { $(this).postLike(); });}); </script> - 在需要显示点赞功能按钮的页面(一般为single.php)放在下面的HTML代码,用于显示点赞功能。
<span 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';?>">喜欢 <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> </span> - 通过以上几次的操作,你就会发现自己的文章页面,已经拥有了点赞功能了。如果要改变它的样式,可以将以下的CSS样式粘贴到自己模板的style.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}
这样就可以在自己建网站时制作出一个wordpress网站点赞功能了。
