我根据米洛的回答制作了一个示例插件,以展示如何使用ajax获取帖子并使用colorbox显示。
实例php:
<?php
/*
Plugin Name: example
*/
function example_ajax_get_post() {
$url = $_POST[\'url\'];
$post = get_post(url_to_postid($url));
?>
<h2><?php print $post->post_title; ?></h2>
<div><?php print $post->post_content; ?></div>
<?php
die();
}// end function
add_action(\'wp_ajax_get_post\', \'example_ajax_get_post\');
function example_init() {
wp_register_style(\'colorbox-style\', plugins_url() . \'/example/css/colorbox.css\');
wp_enqueue_style(\'colorbox-style\');
wp_enqueue_script( \'ajax\', plugin_dir_url( __FILE__ ) . \'js/ajax.js\', array( \'jquery\' ) );
wp_localize_script( \'ajax\', \'ajaxurl\', admin_url( \'admin-ajax.php\' ) );
wp_enqueue_script(\'jquery\');
wp_register_script(\'colorbox-script\', plugins_url() . \'/example/js/jquery.colorbox-min.js\');
wp_enqueue_script(\'colorbox-script\');
}// end function
add_action(\'init\', \'example_init\');
function example_footer() {
?>
<script>
(function($) {
$(document).ready(function() {
$(\'.post .entry-title a\').click(function(e) {
e.preventDefault();
var href = $(this).attr(\'href\');
var data = {
action : \'get_post\',
url : href
};
$.post(ajaxurl, data, function(data) {
$.colorbox({ html : data });
});
});
});
})(jQuery);
</script>
<?php
}// end function
add_action(\'wp_footer\', \'example_footer\');
/* EOF */