可湿性粉剂三方投票系统:说对了!请救救我!

时间:2011-01-14 作者:Lynne

好的,我昨天发布了一个问题,可以找到here.

我在精彩的互联网上找到了一些代码,我认为这些代码确实适合我,但我不知道如何开发成我真正需要的代码。这是明细表-

我需要三个单独的投票链接。我需要把每个词的“投票”改成其他词。IE:好/嗯/不好。(我想我能想出这个办法)

我希望有一种简单易行的方法可以在这段代码中添加另外两个投票链接,因为代码本身非常简单易行。请原谅我缺乏PHP/Jquery知识。

标题代码:

<?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>
投票表决。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参考:

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>\';
}

Source has a demo.

我们将非常非常感谢您的帮助。提前谢谢。

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

这里有一个简单的解决方案。将评分附加到帖子内容的末尾:

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,因为代码非常简单和灵活,并且使用它自己的数据库表。。。

结束

相关推荐

获取在Functions.php中设置的变量,并在我的Custom Post模板中回显它们

在我的函数中设置了以下函数。php文件,以允许我的自定义帖子类型“Slideshow”工作。add_action( \'the_post\', \'paginate_slide\' ); function paginate_slide( $post ) { global $pages, $multipage, $numpages; if( is_single() && get_post_type() == \'lom_s