亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在WordPress短代碼中使用Ajax?

如何在WordPress短代碼中使用Ajax?

PHP
HUWWW 2019-06-29 10:22:39
如何在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個贊

WordPress的短代碼和給出參數的函數一樣,

為了創建Ajax請求,可以在頭文件或函數文件中使用jQuery.ajax或xmlhttpadd_action wp_head鉤子。

您應該在主題文件夾中創建ajax.php,在文件頂部應該包含wp-load.php。并以適當的方式放置所有Ajax函數。


查看完整回答
反對 回復 2019-06-29
  • 3 回答
  • 0 關注
  • 590 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號