Javascript 在两个不同的页面上加载两个不同的AJAX请求
我有一个wordpress网站,在那里我需要两个单独的AJAX请求,它们各自包含不同的部分。然而,它只在两个页面上拉入一个AJAX请求,即使使用了特定的IF-page语句,从技术上讲,这些语句应该禁止它们彼此之间的连接 AJAX在它被调用的两个页面上都能工作,但是两个页面共享相同的AJAX调用,而不是它们自己单独的调用,尽管我做了很多努力 如何才能让front-page.php只调用它的特定AJAX调用,而category.php只调用它自己的调用?现在我有了我觉得应该这样做的代码,但是只有Javascript 在两个不同的页面上加载两个不同的AJAX请求,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我有一个wordpress网站,在那里我需要两个单独的AJAX请求,它们各自包含不同的部分。然而,它只在两个页面上拉入一个AJAX请求,即使使用了特定的IF-page语句,从技术上讲,这些语句应该禁止它们彼此之间的连接 AJAX在它被调用的两个页面上都能工作,但是两个页面共享相同的AJAX调用,而不是它们自己单独的调用,尽管我做了很多努力 如何才能让front-page.php只调用它的特定AJAX调用,而category.php只调用它自己的调用?现在我有了我觉得应该这样做的代码,但是只有ca
cat\u loadmore\u posts
在两个页面上都加载了
首页.php
<?php
$current_page = max( 1, get_query_var( 'paged' ) );
$the_query = new WP_Query( array(
'cat' => 5,
'post_type' => 'post',
'posts_per_page' => 9,
'post_status' => 'publish',
'paged' => $current_page,
) );
$_SESSION['count'] = 1;
wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
'posts' => json_encode( $the_query->query_vars ),
'current_page' => $current_page,
'max_page' => $the_query->max_num_pages
) );
?>
<div id="main" class="container-fluid">
<?php if ($the_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); get_template_part( 'parts/content', get_post_format() ); ?>
<?php $count++; $_SESSION['count']=$_SESSION['count']+1; ?>
<?php if ($count == 8 && is_active_sidebar('seriesbar2') ) : ?>
<div class="post">
<?php dynamic_sidebar('sidebar'); ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
</div><!-- END CONTAINER -->
<?php
$current_page = max( 1, get_query_var( 'paged' ) );
$cat_query = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'post',
'category' => 14,
'post_status'=>'publish',
'paged' => $current_page,
'order'=>'DESC'
) );
$_SESSION['count'] = 1;
wp_localize_script( 'cat_loadmore', 'cat_loadmore_params', array(
'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
'posts' => json_encode( $cat_query->query_vars ),
'current_page' => $current_page,
'max_page' => $cat_query->max_num_pages
) );
?>
<div id="catfeed" class="container feed-container">
<?php if ($cat_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($cat_query->have_posts()) : $cat_query->the_post(); get_template_part( 'parts/categorycontent', get_post_format() ); ?>
<?php $count++; $_SESSION['count']=$_SESSION['count']+1; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div><!-- END CONTAINER -->
jQuery(function($){
var canBeLoaded = true,
bottomOffset = 1300;
$(window).scroll(function(){
if ( cat_loadmore_params.current_page >= cat_loadmore_params.max_page ) {
return;
}
var data = {
'action': 'loadmore',
'query': cat_loadmore_params.posts,
'page' : cat_loadmore_params.current_page
};
if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ){
$.ajax({
url : cat_loadmore_params.ajaxurl,
data: data,
type: 'POST',
beforeSend: function( xhr ){
canBeLoaded = false;
},
success:function(data){
if( data ) {
$('#catfeed').find('div.catpost:last-of-type').after( data );
canBeLoaded = true;
cat_loadmore_params.current_page++;
}
}
});
}
});
});
function misha_my_load_more_scripts() {
if ( is_front_page() )
{
wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/js/myloadmore.js',
array( 'jquery' ), '', true );
wp_enqueue_script( 'my_loadmore' );
}
}
add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );
function cat_load_more_scripts() {
if ( is_category() )
{
wp_register_script( 'cat_loadmore', get_stylesheet_directory_uri() . '/js/catloadmore.js',
array( 'jquery' ), '', true );
wp_enqueue_script( 'cat_loadmore' );
}
}
add_action( 'wp_enqueue_scripts', 'cat_load_more_scripts' );
function register_my_session(){
if( ! session_id() ) {
session_start();
}
}
add_action('init', 'register_my_session');
function cat_loadmore_ajax_handler() {
$args = json_decode( wp_unslash( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // load the next page
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) :
while ( $cat_query->have_posts() ) : $cat_query->the_post();
$count = $_SESSION['count'];
$_SESSION['count']=$_SESSION['count']+1;
get_template_part( 'parts/categorycontent', get_post_format() );
endwhile;
endif;
wp_die();
}
add_action( 'wp_ajax_loadmore', 'cat_loadmore_ajax_handler' ); // Authenticated users
add_action( 'wp_ajax_nopriv_loadmore', 'cat_loadmore_ajax_handler' ); // Non-authenticated users
function misha_loadmore_ajax_handler() {
$args = json_decode( wp_unslash( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // load the next page
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$count = $_SESSION['count'];
$_SESSION['count']=$_SESSION['count']+1;
get_template_part( 'parts/content', get_post_format() );
<?php if ($count == 8 && is_active_sidebar('seriesbar2') ) : ?>
<div class="series-container post second-series">
<?php dynamic_sidebar('seriesbar2'); ?>
</div>
<?php endif;
endwhile;
endif;
wp_die();
}
add_action( 'wp_ajax_loadmore', 'misha_loadmore_ajax_handler' ); // Authenticated users
add_action( 'wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler' ); // Non-authenticated users
catloadmore.js-这个可以在category.php和front-page.php上使用,尽管我没有在front-page.php上调用它。我只想让它在category.php上工作
<?php
$current_page = max( 1, get_query_var( 'paged' ) );
$the_query = new WP_Query( array(
'cat' => 5,
'post_type' => 'post',
'posts_per_page' => 9,
'post_status' => 'publish',
'paged' => $current_page,
) );
$_SESSION['count'] = 1;
wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
'posts' => json_encode( $the_query->query_vars ),
'current_page' => $current_page,
'max_page' => $the_query->max_num_pages
) );
?>
<div id="main" class="container-fluid">
<?php if ($the_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); get_template_part( 'parts/content', get_post_format() ); ?>
<?php $count++; $_SESSION['count']=$_SESSION['count']+1; ?>
<?php if ($count == 8 && is_active_sidebar('seriesbar2') ) : ?>
<div class="post">
<?php dynamic_sidebar('sidebar'); ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
</div><!-- END CONTAINER -->
<?php
$current_page = max( 1, get_query_var( 'paged' ) );
$cat_query = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'post',
'category' => 14,
'post_status'=>'publish',
'paged' => $current_page,
'order'=>'DESC'
) );
$_SESSION['count'] = 1;
wp_localize_script( 'cat_loadmore', 'cat_loadmore_params', array(
'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
'posts' => json_encode( $cat_query->query_vars ),
'current_page' => $current_page,
'max_page' => $cat_query->max_num_pages
) );
?>
<div id="catfeed" class="container feed-container">
<?php if ($cat_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($cat_query->have_posts()) : $cat_query->the_post(); get_template_part( 'parts/categorycontent', get_post_format() ); ?>
<?php $count++; $_SESSION['count']=$_SESSION['count']+1; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div><!-- END CONTAINER -->
jQuery(function($){
var canBeLoaded = true,
bottomOffset = 1300;
$(window).scroll(function(){
if ( cat_loadmore_params.current_page >= cat_loadmore_params.max_page ) {
return;
}
var data = {
'action': 'loadmore',
'query': cat_loadmore_params.posts,
'page' : cat_loadmore_params.current_page
};
if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ){
$.ajax({
url : cat_loadmore_params.ajaxurl,
data: data,
type: 'POST',
beforeSend: function( xhr ){
canBeLoaded = false;
},
success:function(data){
if( data ) {
$('#catfeed').find('div.catpost:last-of-type').after( data );
canBeLoaded = true;
cat_loadmore_params.current_page++;
}
}
});
}
});
});
function misha_my_load_more_scripts() {
if ( is_front_page() )
{
wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/js/myloadmore.js',
array( 'jquery' ), '', true );
wp_enqueue_script( 'my_loadmore' );
}
}
add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );
function cat_load_more_scripts() {
if ( is_category() )
{
wp_register_script( 'cat_loadmore', get_stylesheet_directory_uri() . '/js/catloadmore.js',
array( 'jquery' ), '', true );
wp_enqueue_script( 'cat_loadmore' );
}
}
add_action( 'wp_enqueue_scripts', 'cat_load_more_scripts' );
function register_my_session(){
if( ! session_id() ) {
session_start();
}
}
add_action('init', 'register_my_session');
function cat_loadmore_ajax_handler() {
$args = json_decode( wp_unslash( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // load the next page
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) :
while ( $cat_query->have_posts() ) : $cat_query->the_post();
$count = $_SESSION['count'];
$_SESSION['count']=$_SESSION['count']+1;
get_template_part( 'parts/categorycontent', get_post_format() );
endwhile;
endif;
wp_die();
}
add_action( 'wp_ajax_loadmore', 'cat_loadmore_ajax_handler' ); // Authenticated users
add_action( 'wp_ajax_nopriv_loadmore', 'cat_loadmore_ajax_handler' ); // Non-authenticated users
function misha_loadmore_ajax_handler() {
$args = json_decode( wp_unslash( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // load the next page
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$count = $_SESSION['count'];
$_SESSION['count']=$_SESSION['count']+1;
get_template_part( 'parts/content', get_post_format() );
<?php if ($count == 8 && is_active_sidebar('seriesbar2') ) : ?>
<div class="series-container post second-series">
<?php dynamic_sidebar('seriesbar2'); ?>
</div>
<?php endif;
endwhile;
endif;
wp_die();
}
add_action( 'wp_ajax_loadmore', 'misha_loadmore_ajax_handler' ); // Authenticated users
add_action( 'wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler' ); // Non-authenticated users
functions.php
<?php
$current_page = max( 1, get_query_var( 'paged' ) );
$the_query = new WP_Query( array(
'cat' => 5,
'post_type' => 'post',
'posts_per_page' => 9,
'post_status' => 'publish',
'paged' => $current_page,
) );
$_SESSION['count'] = 1;
wp_localize_script( 'my_loadmore', 'misha_loadmore_params', array(
'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
'posts' => json_encode( $the_query->query_vars ),
'current_page' => $current_page,
'max_page' => $the_query->max_num_pages
) );
?>
<div id="main" class="container-fluid">
<?php if ($the_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); get_template_part( 'parts/content', get_post_format() ); ?>
<?php $count++; $_SESSION['count']=$_SESSION['count']+1; ?>
<?php if ($count == 8 && is_active_sidebar('seriesbar2') ) : ?>
<div class="post">
<?php dynamic_sidebar('sidebar'); ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
</div><!-- END CONTAINER -->
<?php
$current_page = max( 1, get_query_var( 'paged' ) );
$cat_query = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'post',
'category' => 14,
'post_status'=>'publish',
'paged' => $current_page,
'order'=>'DESC'
) );
$_SESSION['count'] = 1;
wp_localize_script( 'cat_loadmore', 'cat_loadmore_params', array(
'ajaxurl' => admin_url( 'admin-ajax.php', 'relative' ),
'posts' => json_encode( $cat_query->query_vars ),
'current_page' => $current_page,
'max_page' => $cat_query->max_num_pages
) );
?>
<div id="catfeed" class="container feed-container">
<?php if ($cat_query->have_posts()) : ?>
<?php $count = 0; ?>
<?php while ($cat_query->have_posts()) : $cat_query->the_post(); get_template_part( 'parts/categorycontent', get_post_format() ); ?>
<?php $count++; $_SESSION['count']=$_SESSION['count']+1; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
</div><!-- END CONTAINER -->
jQuery(function($){
var canBeLoaded = true,
bottomOffset = 1300;
$(window).scroll(function(){
if ( cat_loadmore_params.current_page >= cat_loadmore_params.max_page ) {
return;
}
var data = {
'action': 'loadmore',
'query': cat_loadmore_params.posts,
'page' : cat_loadmore_params.current_page
};
if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ){
$.ajax({
url : cat_loadmore_params.ajaxurl,
data: data,
type: 'POST',
beforeSend: function( xhr ){
canBeLoaded = false;
},
success:function(data){
if( data ) {
$('#catfeed').find('div.catpost:last-of-type').after( data );
canBeLoaded = true;
cat_loadmore_params.current_page++;
}
}
});
}
});
});
function misha_my_load_more_scripts() {
if ( is_front_page() )
{
wp_register_script( 'my_loadmore', get_stylesheet_directory_uri() . '/js/myloadmore.js',
array( 'jquery' ), '', true );
wp_enqueue_script( 'my_loadmore' );
}
}
add_action( 'wp_enqueue_scripts', 'misha_my_load_more_scripts' );
function cat_load_more_scripts() {
if ( is_category() )
{
wp_register_script( 'cat_loadmore', get_stylesheet_directory_uri() . '/js/catloadmore.js',
array( 'jquery' ), '', true );
wp_enqueue_script( 'cat_loadmore' );
}
}
add_action( 'wp_enqueue_scripts', 'cat_load_more_scripts' );
function register_my_session(){
if( ! session_id() ) {
session_start();
}
}
add_action('init', 'register_my_session');
function cat_loadmore_ajax_handler() {
$args = json_decode( wp_unslash( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // load the next page
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) :
while ( $cat_query->have_posts() ) : $cat_query->the_post();
$count = $_SESSION['count'];
$_SESSION['count']=$_SESSION['count']+1;
get_template_part( 'parts/categorycontent', get_post_format() );
endwhile;
endif;
wp_die();
}
add_action( 'wp_ajax_loadmore', 'cat_loadmore_ajax_handler' ); // Authenticated users
add_action( 'wp_ajax_nopriv_loadmore', 'cat_loadmore_ajax_handler' ); // Non-authenticated users
function misha_loadmore_ajax_handler() {
$args = json_decode( wp_unslash( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // load the next page
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
$count = $_SESSION['count'];
$_SESSION['count']=$_SESSION['count']+1;
get_template_part( 'parts/content', get_post_format() );
<?php if ($count == 8 && is_active_sidebar('seriesbar2') ) : ?>
<div class="series-container post second-series">
<?php dynamic_sidebar('seriesbar2'); ?>
</div>
<?php endif;
endwhile;
endif;
wp_die();
}
add_action( 'wp_ajax_loadmore', 'misha_loadmore_ajax_handler' ); // Authenticated users
add_action( 'wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler' ); // Non-authenticated users
function misha\u my\u load\u more\u scripts(){
如果(是首页())
{
wp_register_脚本('my_loadmore',get_stylesheet_directory_uri()。/js/myloadmore.js',
数组('jquery'),'',true);
wp_enqueue_脚本('my_loadmore');
}
}
添加操作(“wp\u排队脚本”、“misha\u我的加载更多脚本”);
函数cat_load_more_scripts(){
如果(是_category())
{
wp_register_脚本('cat_loadmore',get_stylesheet_directory_uri()。/js/catloadmore.js',
数组('jquery'),'',true);
wp_enqueue_脚本('cat_loadmore');
}
}
添加操作('wp_排队_脚本','cat_加载_更多_脚本');
函数寄存器\我的\会话(){
如果(!session_id()){
会话_start();
}
}
添加操作(“初始化”,“注册我的会话”);
函数cat\u loadmore\u ajax\u handler(){
$args=json_decode(wp_反斜杠($_POST['query']),true);
$args['paged']=$\u POST['page']+1;//加载下一页
$cat\u query=新的WP\u查询($args);
如果($cat\u query->have\u posts()):
while($cat_query->have_posts()):$cat_query->the_post();
$count=$\会话['count'];
$\会话['count']=$\会话['count']+1;
获取模板部分('parts/categorycontent',获取发布格式();
结束时;
endif;
wp_die();
}
添加操作('wp_ajax_loadmore','cat_loadmore_ajax_handler');//认证用户
添加操作('wp_ajax_nopriv_loadmore','cat_loadmore_ajax_handler');//未经身份验证的用户
函数misha\u loadmore\u ajax\u handler(){
$args=json_decode(wp_反斜杠($_POST['query']),true);
$args['paged']=$\u POST['page']+1;//加载下一页
$thew_query=newwp_query($args);
如果($the\u query->have\u posts()):
while($the_query->have_posts()):$the_query->the_post();
$count=$\会话['count'];
$\会话['count']=$\会话['count']+1;
获取模板部分('parts/content',获取发布格式());