如何让用户在这个简单的投票计数器(使用帖子元)中向下投票?

时间:2011-04-12 作者:janoChen

This simple tutorial 教你如何使用帖子的元数据制作投票计数器。

我想知道如何在这个小脚本中添加投票(例如,-1,-2)。按以下方式:如果该帖子有0票,但被否决,则最终得到-1票。如果帖子有1票,但被否决,则结果为0(只有一个计数器)。

the jQuery part:

<?php wp_enqueue_script( \'jquery\' ) ?>
<?php wp_head(); ?>
    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery(".vote a").click(

            function() {
                var some = jQuery(this);
                var thepost = jQuery(this).attr("post");
                var theuser = jQuery(this).attr("user");
                jQuery.post("<?php bloginfo(\'template_url\'); ?>/vote.php", {
                    user: theuser,
                    post: thepost
                }, function(data) {
                    var votebox = ".vote" + thepost + " span";
                    jQuery(votebox).text(data);
                    jQuery(some).replaceWith(\'<span class="voted">Voted</span>\');
                });
            });
        });
    </script>
当会员点击投票链接时,上述代码将获得帖子ID和会员的用户ID,并将其发送到名为“投票”的文件中。php使用post方法。投票。php文件将执行添加投票所需的所有操作。

vote.php:

<?php
$file = dirname(__FILE__);
$file = substr($file, 0, stripos($file, "wp-content") );

require( $file . "/wp-load.php");

$currentvotes = get_post_meta($_POST[\'post\'], \'votes\', true);
$currentvotes = $currentvotes + 1;

$voters = get_post_meta($_POST[\'post\'], \'thevoters\', true);
if(!$voters) $voters = $_POST[\'user\']; else $voters = $voters.",".$_POST[\'user\'];

update_post_meta($_POST[\'post\'], \'votes\', $currentvotes);
update_post_meta($_POST[\'post\'], \'thevoters\', $voters);

echo $currentvotes;
?>
信息发布后进行投票。php创建了两个自定义字段。一个用来清点选票,另一个用来将选民添加到列表中,这样他们就不能再投票了。

functions.php:

// voting function
function voting($id) {
global $user_ID;
$currentvotes = get_post_meta($id, \'votes\', true);
$voters = get_post_meta($id, \'thevoters\', true);
$voters = explode(",", $voters);
foreach($voters as $voter) {
    if($voter == $user_ID) $alreadyVoted = true;
}

if(!$currentvotes) $currentvotes = 0;
echo \'<div class="vote vote\'.$id.\'"><span>\'.$currentvotes.\'</span>\';
if($user_ID && !$alreadyVoted) echo \'<br /><a post="\'.$id.\'" user="\'.$user_ID.\'">\'.__("Vote").\'</a>\';
if($user_ID && $alreadyVoted) echo \'<br /><span class="voted">\'.__("Voted").\'</span>\';
echo \'</div>\';
if(!$user_ID) echo \'<div class="signup"><p><a href="\'.get_bloginfo(\'url\').\'/wp-login.php?action=register">\'.__(\'Register\').\'</a> \'.__(\'to vote\').\'.</p></div>\';
}
有什么建议吗?

1 个回复
最合适的回答,由SO网友:Scott 整理而成

你必须再做一个向下投票的按钮。将upvote按钮设置为up类,将down vote按钮设置为down类。

jQuery

<?php wp_enqueue_script( \'jquery\' ) ?>
<?php wp_head(); ?>
    <script type="text/javascript">
        jQuery(document).ready(function() {
            jQuery(".vote a.up").click(

            function() {
                var some = jQuery(this);
                var thepost = jQuery(this).attr("post");
                var theuser = jQuery(this).attr("user");
                jQuery.post("<?php bloginfo(\'template_url\'); ?>/vote.php", {
                    user: theuser,
                    post: thepost,
                    updown: \'up\'
                }, function(data) {
                    var votebox = ".vote" + thepost + " span";
                    jQuery(votebox).text(data);
                    jQuery(some).replaceWith(\'<span class="voted">Voted</span>\');
                });
            });
            jQuery(".vote a.down").click(

            function() {
                var some = jQuery(this);
                var thepost = jQuery(this).attr("post");
                var theuser = jQuery(this).attr("user");
                jQuery.post("<?php bloginfo(\'template_url\'); ?>/vote.php", {
                    user: theuser,
                    post: thepost,
                    updown: \'down\'
                }, function(data) {
                    var votebox = ".vote" + thepost + " span";
                    jQuery(votebox).text(data);
                    jQuery(some).replaceWith(\'<span class="voted">Voted</span>\');
                });
            });
        });
    </script>
投票表决。php

<?php
$file = dirname(__FILE__);
$file = substr($file, 0, stripos($file, "wp-content") );

require( $file . "/wp-load.php");

$currentvotes = get_post_meta($_POST[\'post\'], \'votes\', true);
if("up" == $_POST[\'updown\']) {
    $currentvotes ++;
} elseif("down" == $_POST[\'updown\']) {
    $currentvotes --;
}

$voters = get_post_meta($_POST[\'post\'], \'thevoters\', true);
if(!$voters) $voters = $_POST[\'user\']; else $voters = $voters.",".$_POST[\'user\'];

update_post_meta($_POST[\'post\'], \'votes\', $currentvotes);
update_post_meta($_POST[\'post\'], \'thevoters\', $voters);

echo $currentvotes;
?>
这就是您需要编辑的全部内容

结束