点击按钮只有最后一个帖子发生变化

时间:2015-03-13 19:27:57

标签: php

无论哪个btn我只点击最后一个帖子的计数更改...我如何获得相应的计数更改

    <?php 
    // Query the custom post type to display

        $args = array('post_type' => 'books');
        $query = new WP_Query( $args );
        while ( $query->have_posts() ) : 
                $query->the_post();
                    if ( has_post_thumbnail() ): 
                $postid = get_the_ID();
                $oldcount=get_field('count');
                $newcount=$oldcount+1;                              
    ?>
        <form action="#" method="post">
        <div><?php the_post_thumbnail('thumbnail'); ?><input id="<?php echo $postid;?>" type="submit" name="submit" value="Vote" /><?php echo " ".$oldcount;?></div>
        </form>

    <?php endif; endwhile; ?>

<?php 
    if (isset($_POST['submit'])) { 
    $ID = $_GET['id'];
    echo $ID;
    echo " button clicked";    
    update_field('field_55014a',$newcount,$ID); 
    }
?>

1 个答案:

答案 0 :(得分:1)

您是通过POST提交表单但尝试使用$_GET['id']这与<input id="<?php echo $postid;?>">

不对应

更改表单,以便提交帖子ID:

 <?php
 // THIS PORTION OF THE CODE MUST COME FIRST!!!!!
 if (isset($_POST['submit'])) { 
     $id = $_POST['id'];
     $newcount = // query your database, get the vote count then add one.
     update_field('field_55014a',$newcount,$id); 
     header('Location: '. $_SERVER['PHP_SELF']);
     exit();
 }

 $args = array('post_type' => 'books');
 $query = new WP_Query( $args );
 while ( $query->have_posts() ) {
     $query->the_post();
     if ( has_post_thumbnail() ) {
         $postid = get_the_ID();
         $votecout =get_field('count');
     ?>
         <form action="" method="POST">
             <div>
             <?php the_post_thumbnail('thumbnail'); ?>
             <input type="hidden" value="<?php echo $postid; ?>" name="id" />
             <input type="submit" name="submit" value="Vote" />
             <?php echo $oldcount; ?>
             </div>
         </form>
    <?php
    } // close if
} // close while