这里有一个简单的解决方案。将评分附加到帖子内容的末尾:
add_filter(\'the_content\', \'add_ratings_to_content\', 4);
// remove ratings from excerpts
add_filter(\'get_the_excerpt\', create_function(\'\', \'remove_filter("the_content", "add_ratings_to_content", 6); return;\'), 5);
function add_ratings_to_content($content){
global $post;
if(is_single()) $content .= the_ratings($post->ID);
return $content;
}
显示当前额定值:
function get_current_rating($post_id){
$votes = get_option("votes_{$post_id}");
// handle your rating format here
$output = \'\';
if(isset($_COOKIE["rated_{$post_id}"])) $output .= "Your rating: ".esc_attr($_COOKIE["rated_{$post_id}"]);
if(is_array($votes)):
$output .= isset($votes[\'bad\']) ? "{$votes[\'bad\']} people think this sucks." : null;
$output .= isset($votes[\'meh\']) ? "{$votes[\'meh\']} people said meh." : null;
$output .= isset($votes[\'good\']) ? "{$votes[\'good\']} people think this post was good." : null;
else:
$output = "no votes yet";
endif;
return $output;
}
ajax请求。使用
$_SERVER[\'REMOTE_ADDR\']
对多个投票进行IP检查,因为Cookie可以轻松删除(并将IP+post id存储在某个位置,如
transient)
add_action(\'init\', \'process_vote\');
function process_vote() {
if($_GET[\'vote\']):
$rating = esc_attr($_GET[\'vote\']);
$post_id = esc_attr($_GET[\'post_id\']);
$already_voted = isset($_COOKIE["rated_{$post_id}"]) ? true : false;
$current_rating = get_current_rating($post_id);
if ($post_id && in_array($rating, array(\'bad\', \'meh\', \'good\')) && !$already_voted):
// update db
$votes = get_option("votes_{$post_id}");
$votes[$rating] = isset($votes[$rating]) ? ($votes[$rating]+1) : 1;
update_option("votes_{$post_id}", $votes);
setcookie("rated_{$post_id}", $rating, time() + (86400 * 30)); // 30 day cookie
echo get_current_rating($post_id);
else:
echo "Already voted?";
endif;
die();
endif;
}
分级的HTML,简单的东西,没有css:
function the_ratings($post_id = false, $disabled = false){
$already_voted = isset($_COOKIE["rated_{$post_id}"]) ? true : false;
ob_start(); ?>
<?php if(!$already_voted && !$disabled): ?>
<p>How would you rate this?</p>
<ul class="vote-process">
<li><a rel="<?php echo $post_id; ?>">bad</a></li>
<li><a rel="<?php echo $post_id; ?>">meh</a></li>
<li><a rel="<?php echo $post_id; ?>">good</a></li>
</ul>
<?php endif; ?>
<div class="vote-status">
<?php echo get_current_rating($post_id); ?>
</div>
<?php
return ob_get_clean();
}
页面页脚中包含的JavaScript。
add_action("wp_footer", "ratings_js");
function ratings_js(){ ?>
<script type="text/javascript">
/* <![CDATA[ */
jQuery(document).ready(function($){
$(".vote-process a").click(function () {
var id = $(this).attr(\'rel\');
var post = $("#post-"+id);
var link = $(this).html();
$.ajax({
type: "GET",
url: "<?php echo home_url(\'/\'); ?>",
data: {
post_id: id,
vote: link
},
beforeSend: function() {
post.find(".vote-status").html("Please wait...");
},
success: function(response){
post.find(".vote-status").html(response);
post.find(".vote-process").remove();
}
});
});
});
/* ]]> */
</script>
<?php
}
删除帖子时,请删除votes\\u posted选项:
add_action(\'delete_post\', \'remove_votes\');
function remove_votes($post_id){
delete_option("votes_{$post_id}");
return $post_id;
}
我仍然建议使用CDNvote,因为代码非常简单和灵活,并且使用它自己的数据库表。。。