如何在WordPress短代码中使用Ajax?
/wp-content/themes/%your_theme%/js/ajax-load-quote.php
<?php /* uncomment the below, if you want to use native WP functions in this file */// require_once('../../../../wp-load.php'); $array = file( $_POST['file_path'] ); // file path in $_POST, as from the js $r = rand( 0, count($array) - 1 ); return '<p>' . $array[$r] . '</p>'; ?>
<div id="randomquotes"> <p>I would rather have my ignorance than another man’s knowledge, because I have so much more of it.<br /> -- Mark Twain, American author & Playwright</p></div><a id="newquote" class="button" href="#" title="Gimme a new one!">New Quote</a>
/wp-content/themes/%your_theme%/js/ajax-load-quote.js
function ajaxQuote() { var theQuote = jQuery.ajax({ type: 'POST', url: ajaxParams.themeURI+'js/ajax-load-quote.php', /* supplying the file path to the ajax loaded php as a $_POST variable */ data: { file_path: ajaxParams.filePath }, beforeSend: function() { ajaxLoadingScreen(true,'#randomquotes'); }, success: function(data) { jQuery('#randomquotes').find('p').remove(); jQuery('#randomquotes').prepend(data); }, complete: function() { ajaxLoadingScreen(false,'#randomquotes'); } });
ABOUTYOU