我试图将“加载更多帖子”功能应用于帖子循环,但在管理ajax时,我处理的是400个错误请求。参考php。
我使用的参考是—https://rudrastyh.com/wordpress/load-more-posts-ajax.html
以下函数(在functions.php中)正在将查询参数传递给javascript:
function wordpress_my_load_more_scripts()
{
global $wp_query;
wp_enqueue_script(\'jquery\');
wp_register_script( \'my_loadmore\', get_stylesheet_directory_uri() . \'/myloadmore.js\', array(\'jquery\') );
wp_localize_script( \'my_loadmore\', \'wordpress_loadmore_params\', array(
\'ajaxurl\' => admin_url() . \'admin-ajax.php\',
\'posts\' => json_encode( $wp_query->query_vars ),
\'current_page\' => get_query_var( \'paged\' ) ? get_query_var(\'paged\') : 1,
\'max_page\' => $wp_query->max_num_pages
) );
wp_enqueue_script( \'my_loadmore\' );
}
add_action( \'wp_enqueue_scripts\', \'wordpress_my_load_more_scripts\' );
参数传递给以下名为“myloadmore.js”的jQuery脚本:
jQuery(function($){
$(\'.wordpress_loadmore\').click(function()
{
var button = $(this),
data = {
\'action\': \'loadmore\',
\'query\': wordpress_loadmore_params.posts,
\'page\' : wordpress_loadmore_params.current_page
};
console.log(wordpress_loadmore_params.ajaxurl);
$.ajax({
url : wordpress_loadmore_params.ajaxurl, // AJAX handler
data : data,
type : \'POST\',
beforeSend : function ( xhr )
{
button.text(\'Loading...\');
},
success : function( data ){
if( data ) {
button.text( \'More posts\' ).prev().before(data);
wordpress_loadmore_params.current_page++;
if ( wordpress_loadmore_params.current_page == wordpress_loadmore_params.max_page )
button.remove(); // if last page, remove the button
} else {
button.remove(); // if no data, remove the button as well
}
}
});
});
});
函数内部的以下函数。php预计将在while循环中提供另外三个帖子:
function wordpress_loadmore_ajax_handler()
{
$args = json_decode( stripslashes( $_POST[\'query\'] ), true );
$args[\'paged\'] = $_POST[\'page\'] + 1;
$args[\'post_status\'] = \'publish\';
query_posts( $args );
if(have_posts() ) :
echo "We have post(s)!";
while( have_posts() ): the_post();
echo "A post!";
endwhile;
endif;
die;
}
add_action(\'wp_ajax_loadmore\', \'wordpress_loadmore_ajax_handler\');
add_action(\'wp_ajax_nopriv_loadmore\', \'wordpress_loadmore_ajax_handler\');
post循环如下:
<ul class="products columns-3">
<?php
$query_params = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 3
);
$wp_query = new WP_Query( $query_params);
if( $wp_query->have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<li class="product post-item">
<span class="post-image">
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail())
{
the_post_thumbnail();
}
?>
</a>
</span>
<h2 class="post-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<span class="post-category"><?php the_category(\', \');?></span>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
<nav>
<?php
global $wp_query; // you can remove this line if everything works for you
// don\'t display the button if there are not enough posts
if ( $wp_query->max_num_pages > 1 )
echo \'
<div class="wordpress_wrapper">
<div class="wordpress_loadmore">More posts</div>
</div>\'; // you can use <a> as well
?>
</nav>
<?php wp_reset_postdata(); ?>
单击按钮可加载更多帖子,结果显示以下消息:
https://www.uvjagtpro.dk/wp-admin/admin-ajax.php
jquery.js?ver=1.12.4:4 POST https://www.uvjagtpro.dk/wp-admin/admin-ajax.php 400 ()
send @ jquery.js?ver=1.12.4:4
ajax @ jquery.js?ver=1.12.4:4
(anonymous) @ myloadmore.js:13
dispatch @ jquery.js?ver=1.12.4:3
r.handle @ jquery.js?ver=1.12.4:3
为什么我不能解析名为“wordpress\\u loadmore\\u params.ajaxurl”的数组中的变量,而不导致400个错误请求?
此处是指向页面的链接-https://www.uvjagtpro.dk/arkiv/