你在这里发布的脚本是什么?我希望是这样not ajax请求将调用的脚本,不是吗??
通常,ajax请求的php脚本如下所示:
<?php
$output = array();
$posts = new WP_Query( array( \'showposts\' => 20 ) );
foreach ( $posts as $post ) {
array_push( $output, $post->title );
}
header( \'Content-type: application/json\' );
die( json_encode( $output ) );
在JavaScript中,您可以检索json编码的数据,对其进行解码并用JavaScript打印出来。如果需要访问WordPress函数,可以使用输出缓冲。
<?php
$output = array();
$posts = new WP_Query( array( \'showposts\' => 20 ) );
foreach ( $posts as $post ) {
array_push( $output, $post->title );
}
header( \'Content-type: application/json\' );
die( json_encode( $output ) );
现在,您可以访问JS中的数据:
$.post(
adminurl,
data,
function( response ) {
$(\'#outputdiv\').apend(\'<ul id="outputlist"></ul>\');
for( var elem in response ) {
$(\'#outputlist\').append(\'<li>\' + response[elem] + \'</li>\');
}
}
);
如果您需要一些WordPress函数,也可以使用outputbuffering:
<?php
$output = \'\';
$posts = new WP_Query( array( \'showposts\' => 20 ) );
ob_start();
echo \'<ul>\';
foreach ( $posts as $post ) {
printf( \'<li><a href="%s">%s</a></li>\', get_permalink( $post->ID ), $post->title );
}
echo \'</ul>\';
$output = ob_get_clean();
header( \'Content-type: application/json\' );
die( json_encode( array( \'output\' => $output ) ) );
并打印出全部数据:
$.post(
adminurl,
data,
function( response ) {
$(\'#outputdiv\').apend(response.output);
}
);
但是,减少ajax请求传输的数据量始终是一个非常好的主意。因此,最好只返回纯post数据,而不是html。
您的脚本可能如下所示(未测试!)
PHP:
<?php
$posts = new WP_Query( array( \'showposts\' => 20 ) );
$data = array();
foreach ( $posts as $post ) {
$element = array(
\'thumbnail\' => \'\',
\'permalink\' => \'\',
\'catlist\' => \'\',
\'title\' => \'\',
\'time\' => \'\',
);
if ( has_post_thumbnail( $post->ID ) )
$element[\'thumbnail\'] = get_the_post_thumbnail( $post->ID, \'sidethumb\' );
ob_start();
echo wp_sprintf_l(
\'%l\',
wp_list_pluck( get_the_category( $post->ID ), \'cat_name\')
);
$element[\'catlist\'] = ob_get_clean();
ob_start();
echo human_time_diff( get_the_time(\'U\'), current_time(\'timestamp\') ) . \' ago\';
$element[\'time\'] = ob_get_clean();
$element[\'permalink\'] = esc_attr( get_permalink( $post->ID ) );
$element[\'title\'] = esc_html( $post->title );
array_push( $data, $element );
}
wp_reset_query();
header( \'Content-type: application/json\' );
die( json_encode( $data ) );
JS公司:
var data = {};
$.post(
adminurl,
data,
function( response ) { printSidebarPosts(response); }
);
function printSidebarPosts( response ) {
var content = \'\';
for ( var i in response ) {
var out = response[i];
var thumbdiv = ( out.thumbnail != \'\' ) ? \'<div class="figure">%thumbnail%</div>\' : \'\';
var elem =
\'<li>%thumbdiv%\'+
\'<a href="%permalink%">\'+
\'<span class="theparentcat">%catlist%</span>\'+
\'<h2>%title%</h2>\'+
\'<span class="thetime">%time%</span>\'+
\'</a></li>\'
.replace( /%thumbdiv%/g, thumbdiv )
.replace( /%permalink%/g, out.permalink )
.replace( /%catlist%/g, out.catlist )
.replace( /%title%/g, out.title )
.replace( /%time%/g, out.time );
content = content + elem;
}
var sidebarcontent = \'<ul>%content%</ul>\'.replace( /%content%/g, content );
$(\'#sidebar\').html( sidebarcontent );
}
在PHP部分,您应该只收集数据。在JavaScript部分,创建HTML。这大大减少了传输的数据量,加快了ajax请求的速度。
UPDATE
添加注释中要求的低级基础知识。
但是我如何放置一个可以点击的按钮。。。
<input type="button" id="get_more_posts_ajax_trigger" value="Show more posts" />
<a href="#" id="get_more_posts_ajax_trigger">Show more posts</a>
<div id="get_more_posts_ajax_trigger">Show more posts</div>
有三种可能的元素可以显示您可以单击的内容。在JavaScript中,您必须捕获此单击。
jQuery(document).ready(
function($){
$(\'#get_more_posts_ajax_trigger\').click(
function(){
var data = {
\'action\' : [the ajax hook you registered in WordPress with add_action( \'wp_ajax_[action]\', ... ) and add_action( \'wp_ajax_nopriv_[action]\', ...)]
};
$.post( adminurl, data, function(response) { [see codeexamples above] } );
};
);
);
顺便说一下,php在我的页面上完全搞砸了may的布局
你把PHP代码放进了你的页面?好啊你必须把它变成一个函数。您必须在WordPress中将此函数注册为ajax回调。使用add_action( \'wp_ajax_[action]\', \'YourFunctionNameHere\' );
和add_action( \'wp_ajax_nopriv_[action]\', \'YourFunctionNameHere\' );
你必须阅读this Codex page 关于ajax和此Codex page about how to implement ajax 插件中。尤其是零件Ajax on the Viewer-Facing Side.