点击后<a>
tage发送ajax请求。我将只写php代码做jQuery
分开你自己。
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
action: \'my_action\'
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
//Do watever after you get response
});
});
</script>
在您的
function.php
add_action( \'wp_ajax_my_action\', \'my_action\' );
add_action( \'wp_ajax_nopriv_my_action\', \'my_action\' );
function my_action(){
global $current_user;
get_currentuserinfo();
$userid = $current_user->ID; //You can also user get_current_user_id() to get current user id
$args = array(
\'posts_per_page\' => \'-1\',
\'post_type\' => \'answer\',
\'post_status\' => \'publish\',
\'author__not_in\' => $userid, //exclude user posts
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'question_owner_id\'
\'value\' => $userid, //only show posts for the user
\'compare\' => \'=\'
),
array(
\'key\' => \'_read_answer\'
\'value\' => \'unread\', //only show unread posts
\'compare\' => \'=\'
),
)
);
$questions = new WP_Query( $args ); //Get all the post whose meta you want to update
foreach($questions->posts as $question):
update_post_meta( $question->ID, \'_read_answer\', \'read\' ); //Update all the posts meta.
endforeach;
echo true; //Send the response. You can aslo send response whatever you want.
die();
}
当有人点击
a
标记它将向服务器发送ajax请求。服务器运行前面的代码以获取其
meta
您要更新。收到帖子后,更新他们的
meta
价值