wordpress插件如何点击一次upvote和downvote按钮

时间:2014-05-14 13:54:34

标签: wordpress button plugins

嗨,我创建了一个wordpress插件,它创建了两个用于计算upvotes的post变量,用于计算downvotes的第二个。

请告诉我如何点击一次这些。在这里,我可以在刷新页面后点击这些按钮。

function display_bar_content( $content ) {
    if ( is_single() ) {
        global $post;
        $current_views = get_post_meta($post->ID, "post_views", true);
        if ( !isset($current_views) OR empty($current_views) OR !is_numeric($current_views) ) {
            $current_views = 0;
        }
?>

<script>
jQuery(document).ready(function(){
    jQuery(".upvote").click(function(){

        <?php // execute this code after pressing upvote 
            $upvote = get_post_meta($post->ID, "upvote", true);
            if(!isset($upvote) OR empty($upvote) OR !is_numeric($upvote) ) {
                $upvote = 0;
            }

            $upvote ++;
            update_post_meta($post->ID, "upvote",$upvote );
        ?>

        jQuery(".upvote").html("Upvote : <?php echo ++$upvote; ?> ");
    });


    jQuery(".downvote").click(function(){

        <?php // execute this code after pressing downvote

        $downvote = get_post_meta($post->ID, "downvote", true);

        if(!isset($downvote) OR empty($downvote) OR !is_numeric($downvote) ) {
            $downvote = 0;
        }

        $downvote ++;

        update_post_meta($post->ID, "downvote",$downvote );

        ?>

        jQuery(".downvote").html("Downvote : <?php echo ++$downvote; ?> ");

    });

});

</script>


<?php 

$upvote = get_post_meta($post->ID, "upvote", true);

if(!isset($upvote) OR empty($upvote) OR !is_numeric($upvote) ) {
    $upvote = 0;
}
$downvote = get_post_meta($post->ID, "downvote", true);

if(!isset($downvote) OR empty($downvote) OR !is_numeric($downvote) ) {
    $downvote = 0;
}

$write = '<div id="infobar"> <span class="views"> '.$current_views.'</span> 
<span class="upvote">'.$upvote.' </span> <span class="downvote"> '.$downvote.' </span>
</div>';

$content =  $content .$write ;
return $content;
}

}

add_action('the_content', 'display_bar_content' ); 

0 个答案:

没有答案