在两个不同的页面上加载两个不同的AJAX请求

时间:2019-04-20 07:39:36

标签: javascript php jquery ajax wordpress

我有一个wordpress网站,在这里我需要两个单独的AJAX请求,它们分别包含不同的部分。但是,即使使用特定的IF页面语句,从技术上来讲,它们应该互相禁止,但它只会在两个页面上拉入一个AJAX请求。

AJAX在被调用的两个页面上都有效,但是尽管我努力,但两个页面都共享 SAME AJAX调用,而不是它们自己的单独调用。

我怎样才能让front-page.php只调用它的SPECIFIC AJAX调用,而category.php只调用它自己的调用呢?现在,我拥有应该执行的代码,但是在两个页面上仅加载cat_loadmore_posts

front-page.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 -->

category.php

<?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 -->

myloadmore.js-我添加catloadmore.js后无法使用的AJAX调用-以前可以使用

jQuery(function($){
    var canBeLoaded = true,
        bottomOffset = 1300; 

    $(window).scroll(function(){
        if ( misha_loadmore_params.current_page >= misha_loadmore_params.max_page ) {
            return;
        }
        var data = {
            'action': 'loadmore',
            'query': misha_loadmore_params.posts,
            'page' : misha_loadmore_params.current_page
        };
        if( $(document).scrollTop() > ( $(document).height() - bottomOffset ) && canBeLoaded == true ){
            $.ajax({
                url : misha_loadmore_params.ajaxurl,
                data: data,
                type: 'POST',
                beforeSend: function( xhr ){
                    canBeLoaded = false;
                },
                success:function(data){
                    if( data ) {
                        $('#main').find('div.post:last-of-type').after( data );
                        canBeLoaded = true; 
                        misha_loadmore_params.current_page++;
                    }
                }
            });
        }
    });
});

catloadmore.js-即使我没有在front-page.php上调用它,它也可以在category.php和front-page.php上运行。我只希望它在category.php

上工作
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++;
                    }
                }
            });
        }
    });
});

functions.php

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

0 个答案:

没有答案