{"id":654928,"date":"2019-08-29T11:15:03","date_gmt":"2019-08-29T14:15:03","guid":{"rendered":"https:\/\/xptt.com\/?p=654928"},"modified":"2019-09-16T12:22:30","modified_gmt":"2019-09-16T15:22:30","slug":"wordpress-ajax%e5%8a%a0%e8%bd%bd%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/xptt.com\/654928.html","title":{"rendered":"WordPress Ajax\u52a0\u8f7d\u5b9e\u4f8b"},"content":{"rendered":"<p>\u6709\u4eba\u95ee\u6211\u8fd9\u4e2a\u4e3b\u9898\u7684\u9876\u90e8Ajax\u65e0\u5237\u65b0\u52a0\u8f7d\u8bc4\u8bba\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u7b80\u5355\u6574\u7406\u4e00\u4e0b\uff0c\u770b\u4e0b\u4ee3\u7801\u5e94\u8be5\u5c31\u660e\u767d\u4e86\u3002<\/p>\n<p>jQuery\u4ee3\u7801\uff1a<\/p>\n<blockquote><p>\/\/\u6700\u65b0\u8bc4\u8bba<br \/>\n$('#lianxi').live('click', function(e) { \/\/\u901a\u8fc7\u9876\u90e8lianxi\u6309\u94ae\u6765\u89e6\u53d1<br \/>\ne.preventDefault();<\/p>\n<p>function ajaxs() {<br \/>\n$.ajax({<br \/>\nurl: '\/?action=ajax',<br \/>\ntype: 'get',<br \/>\nbeforeSend: function() {<br \/>\n$('#ajaxpinglun').empty().html('&lt;div style=\"text-align:center;padding:20px;\"&gt;Ajax\u52a0\u8f7d\u4e2d....&lt;\/div&gt;') \/\/\u663e\u793aloading<br \/>\n},<br \/>\nerror: function(a) {<br \/>\n$('#ajaxpinglun').hide().empty().html('&lt;div style=\"text-align:center;padding:20px;\"&gt;\u6ca1\u52a0\u8f7d&lt;\/div&gt;').fadeIn('fast') \/\/\u9519\u8bef\u65f6\u8f93\u51fa<br \/>\n},<br \/>\nsuccess: function(a) {<br \/>\n$('#ajaxpinglun').empty().html(a) \/\/\u6210\u529f\u65f6\u8f93\u51fa<br \/>\n}<br \/>\n});<br \/>\nreturn false<br \/>\n};<br \/>\najaxs();<br \/>\n});<\/p><\/blockquote>\n<p>\u9875\u9762\uff1a<\/p>\n<blockquote><p>&lt;div id=\"lianxi\"&gt;&lt;\/div&gt; --\u8fd9\u4e2a\u662f\u6309\u94ae<\/p>\n<p>&lt;div class=\"lxpanel\"&gt;&lt;div id=\"ajaxpinglun\"&gt;&lt;\/div&gt;&lt;\/div&gt; --\u663e\u793a\u7684div\u5c42<\/p><\/blockquote>\n<p>php function\u83b7\u53d6\u6570\u636e\uff1a<\/p>\n<blockquote><p>##\u8bc4\u8bba\u6392\u884c\u548c\u8bc4\u8bba<br \/>\nfunction AjaxLoad() {<br \/>\nif (isset($_GET['action'])) {<br \/>\nif ($_GET['action'] == 'ajax') {<br \/>\n\/\/\u83b7\u53d6\u6700\u65b0\u8bc4\u8bba\u4ee3\u7801\u6216\u60f3\u663e\u793a\u7684\u5185\u5bb9<\/p>\n<p>echo '&lt;div class=\"rc_comments\"&gt;';<br \/>\nglobal $wpdb;<br \/>\n$sql = \"SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url,comment_author_email, SUBSTRING(comment_content,1,16) AS com_excerpt FROM $wpdb-&gt;comments LEFT OUTER JOIN $wpdb-&gt;posts ON ($wpdb-&gt;comments.comment_post_ID = $wpdb-&gt;posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' AND user_id='0' AND comment_author != '\u90d1\u6c38' ORDER BY comment_date_gmt DESC LIMIT 6\";<br \/>\n$comments = $wpdb-&gt;get_results($sql);<br \/>\n\/\/$comment-&gt;comment_content=preg_replace('#\\[img\\](.*)\\[\/img\\]#','[\u56fe]',$comment-&gt;comment_content);<br \/>\n$output = $pre_HTML;<br \/>\nforeach ($comments as $comment) {<br \/>\n$output .= \"\\n&lt;li&gt;\".get_avatar($comment, 32,'',$comment-&gt;comment_author).\" &lt;a href=\\\"\" . get_permalink($comment-&gt;ID) .\"#comment-\" . $comment-&gt;comment_ID . \"\\\" title=\\\"\u53d1\u8868\u5728\uff1a \" .$comment-&gt;post_title . \"\\\"&gt;\" .strip_tags($comment-&gt;comment_author).\":&lt;br\/&gt;\". strip_tags($comment-&gt;com_excerpt).\"&lt;\/a&gt;&lt;br \/&gt;&lt;\/li&gt;\";<br \/>\n}<br \/>\n$output .= $post_HTML;<br \/>\necho $output;<br \/>\necho '&lt;\/ul&gt;';<br \/>\ndie;<\/p>\n<p>}<br \/>\n}<br \/>\n}<br \/>\nadd_action('init', 'AjaxLoad');<\/p>\n<p>&nbsp;<\/p><\/blockquote>\n<p>CSS:<\/p>\n<blockquote><p>#ajaxpinglun {<br \/>\npadding:0 15px 15px;<br \/>\nmax-width:280<br \/>\n}<\/p>\n<p>.lxpanel {<br \/>\nposition:absolute;<br \/>\ntop:62px;<br \/>\nright:45px;<br \/>\nborder:1px solid #DDD;<br \/>\nz-index:999<br \/>\n}<\/p>\n<p>#lianxi {<br \/>\nposition:absolute;<br \/>\ntop:17px;<br \/>\nright:90px;<br \/>\nwidth:23px;<br \/>\nheight:20px;<br \/>\nborder:1px solid #999;<br \/>\ncursor:pointer;<br \/>\nz-index:999;<br \/>\nborder-radius:4px<br \/>\n}<br \/>\n#lianxi:before {<br \/>\ndisplay:block;<br \/>\nposition:absolute;<br \/>\nleft:20%;<br \/>\ntop:100%;<br \/>\nwidth:0;<br \/>\nheight:0;<br \/>\nborder-left:9px solid #999;<br \/>\nborder-right:7px solid transparent;<br \/>\nborder-bottom:7px solid transparent;<br \/>\ncontent:''<br \/>\n}<\/p><\/blockquote>\n<p>PS\uff1a\u540e\u6765\u53c8\u4e0a\u4f20\u4e86\u5177\u4f53\u4ee3\u7801\uff0c\u8bd5\u8bd5\u5427\uff0c\u5e94\u8be5\u53ef\u4ee5\u3002\u5982\u679c\u8fd8\u662f\u4e0d\u884c\uff0c\u4f60\u8fd8\u662f\u770b\u8fd9\u4e2a\u6559\u7a0b\u5427\uff1ahttps:\/\/www.google.com.hk\/search?hl=zh-CN&amp;q=wordpress ajax\u52a0\u8f7d\u6570\u636e\uff0c\u6309\u94ae\u548ccss\u663e\u793a\u90e8\u5206\u53c2\u8003\u672c\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u4eba\u95ee\u6211\u8fd9\u4e2a\u4e3b\u9898\u7684\u9876\u90e8Ajax\u65e0\u5237\u65b0\u52a0\u8f7d\u8bc4\u8bba\u662f\u5982\u4f55\u5b9e\u73b0\u7684\uff0c\u7b80\u5355\u6574\u7406\u4e00\u4e0b\uff0c\u770b\u4e0b\u4ee3\u7801\u5e94\u8be5\u5c31\u660e\u767d\u4e86\u3002 jQuery\u4ee3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1181,733,1246],"views":13577,"_links":{"self":[{"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/posts\/654928"}],"collection":[{"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/comments?post=654928"}],"version-history":[{"count":0,"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/posts\/654928\/revisions"}],"wp:attachment":[{"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/media?parent=654928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/categories?post=654928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xptt.com\/wp-json\/wp\/v2\/tags?post=654928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}