如何在WordPress短代碼中使用Ajax?我有個代碼可以顯示一個隨機引號。一個人編寫了一個函數來實現所有這些。但是,由于某種原因,通過Ajax更新數據不起作用。當你按下“新引號”按鈕時,什么都不會發生。也許有人知道原因?在下面的代碼中需要修正什么,以便當您單擊“新引號”時加載一個新的引號?PHP/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>';
?>HTML結構在頁面內容、小部件或模板文件中:<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>顯然你可以根據你的喜好來調整,但是為了這個例子,這就是我們要做的。稍后我們將通過一個短代碼生成上面的內容。jQuery/wp-content/themes/%your_theme%/js/ajax-load-quote.jsfunction 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');
}
});如何使用WordPress中的Ajax更新頁面上的內容?
3 回答

ABOUTYOU
TA貢獻1812條經驗 獲得超5個贊
add_action wp_head
- 3 回答
- 0 關注
- 590 瀏覽
添加回答
舉報
0/150
提交
取消