可能有一种更简单的方法可以做到这一点。在多次尝试从url获取postid后,我选择将id添加到永久链接。但是,我不知道在不刷新页面的情况下如何更新自定义值。
滑动分页
$(document).ready(function(){
$("a").addClass("external");
$("a").attr( "target", "_blank" );
});
jQuery(document).ready(function($) {
$(\'.external\').click(function(event) {
// had to add the post id to the permalink in loop as url to postid didn\'t work
var currentID = $(this).attr(\'id\');
var data = {
action: \'my_action\',
whatever: currentID
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(response) {
//update the custom field value without a page refresh
});
});
});
功能。php
add_action(\'wp_ajax_my_action\', \'my_action_callback\');
function my_action_callback() {
global $wpdb; // this is how you get access to the database
$whatever = intval( $_POST[\'whatever\'] );
// get number of views if we have any
$views = get_post_meta($whatever, views, true);
// update or add post meta
if(!update_post_meta($whatever, \'views\', ($views+1))) {
add_post_meta($whatever, \'views\', 1, true);
}
die(); // this is required to return a proper result
}
内容。php
<a href="<?php the_permalink(); ?>" id="<?php the_ID(); ?>"><?php the_title(); ?></a>
<?php echo get_post_meta($post->ID, \'views\', true) ?>
// update the view ?