mercredi 25 février 2015

Add a counter for mouseovers (custom field)


I am trying to count mouseovers on the div for each post. The div is inside the loop. I am almost there, I think you might have to just help me correct some mistake.


The div and jquery. Notice that I am using the the_ID() to give the div an CSS id, which this allows the Jquery to target it. It uses AJAX to pass the post id to addHit.php



<div class="loop-entry-details" id="post<?php echo the_ID() ?>">
<script>
$('#post<?php echo the_ID() ?>').mouseover(function(){
postID = <?php echo the_ID() ?>;
$.ajax({
type: "POST",
url: "addHit.php",
data:{ post_id: postID },
success: function(data){
console.log(data);
}
})
}
</script>
</div>


addHit.php located in the theme root



$post_id = $_POST['post_id'];

function addHit2($post_id){
$key = 'hits';
$themeta = get_post_meta($post->ID, $key, TRUE);


if($themeta != '') {
add_post_meta($post_id, $key, 1);
} else {
update_post_meta($post_id, $key, 1);
}
}
addHit2($post_id);


Doesn't seem to work. What's wrong?





Aucun commentaire:

Enregistrer un commentaire