WordPress-Ajax过滤器/ Loadmore无法正常工作

时间:2019-01-04 11:45:25

标签: php ajax wordpress post filter

我对另一个帖子查询具有相同的ajax函数,并且没有任何问题! 我复制了所有这些文件以创建另一个后置查询,但是没有用,我不知道为什么!

我更改了每个函数的名称和脚本,我得到的错误是

  

意外值\“ 0解析viewBox属性。admin-ajax.php

     

解析x属性的意外值\“ 0 \”。 admin-ajax.php

     

解析y属性的意外值\“ 0 \”。 admin-ajax.php

这是怎么了?

function.php(wp_enqueue_scripts)。

function misha_my_load_more_learning_scripts() {

    global $wp_query; 

    wp_register_script( 'misha_learning_filter_scripts', get_stylesheet_directory_uri().'/custom-js/myloadmorelearning.js', array('jquery'), '1.0.0', true );
    wp_enqueue_script( 'misha_learning_filter_scripts' );

    wp_localize_script( 'misha_learning_filter_scripts', 'misha_loadmore_learning_params', array(
        'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
        'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
        'current_page' => $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1,
        'max_page' => $wp_query->max_num_pages
    ) );
}

add_action( 'wp_enqueue_scripts', 'misha_my_load_more_learning_scripts', 1 );

function.php(misha_learning_filter_function)

function misha_learning_filter_function(){

$args = array(
    'orderby' => 'date',
    'post_type' => 'post',
    'post_status' => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page' => 18
);

      if( isset( $_POST['categoryfilter6'] ) )
            $args['tax_query'] = array(
                array(
                    'hide_empty' => 0,
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => $_POST['categoryfilter6'],
                )
       ); 

    $l_query = new WP_Query( $args );

    if( $l_query->have_posts() ) :

        ob_start(); 

            $archive_content_layout = get_option( 'archive_content_layout', 'th-grid-2' );
            echo '<div class="posts-wrap ' . esc_attr( $archive_content_layout ) . '">';

        while( $l_query->have_posts() ): $l_query->the_post();

            get_template_part( 'template-parts/content', get_post_format() );


        endwhile;

            echo '</div><!-- .posts-wrap -->';
        wp_reset_postdata();
        $content = ob_get_contents(); // we pass the posts to variable
        ob_end_clean(); // clear the buffer

    else :
    ob_start(); // start the buffer to capture the output of the template
    get_template_part( 'template-parts/content', 'none' );
    $content = ob_get_contents(); // pass the output to variable
    ob_end_clean();

    endif;

    echo json_encode( array(
        'posts' => json_encode( $l_query->query_vars ),
        'max_page' => $l_query->max_num_pages,
        'found_posts' => $l_query->found_posts,
        'content' => $content
    ) );

    die();
}
add_action('wp_ajax_mylearningfilter', 'misha_learning_filter_function'); 
add_action('wp_ajax_nopriv_mylearningfilter', 'misha_learning_filter_function');

function.php(misha_loadmore_learning_ajax_handler)。

add_action('wp_ajax_loadmorelearningbutton', 'misha_loadmore_learning_ajax_handler');
add_action('wp_ajax_nopriv_loadmorelearningbutton', 'misha_loadmore_learning_ajax_handler');

function misha_loadmore_learning_ajax_handler(){

    $args = json_decode( stripslashes( $_POST['query'] ), true );
    $args['paged'] = $_POST['page'] + 1; 
    $args['post_status'] = 'publish';

$l_query = new WP_Query( $args );

    global $wp_query;

    if( $l_query->have_posts() ) :

            $archive_content_layout = get_option( 'archive_content_layout', 'th-grid-2' );
            echo '<div class="posts-wrap ' . esc_attr( $archive_content_layout ) . '">';

        while( $l_query->have_posts() ): $l_query->the_post();

            get_template_part( 'template-parts/content', get_post_format() );


        endwhile;

            echo '</div><!-- .posts-wrap -->';

    else :
        get_template_part( 'template-parts/content', 'none' );
    endif;

    die; 
}

template.php(窗体)。

    <form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="misha_learning_filters">

        <?php

            if( $terms = get_terms( 'category', 'orderby=name&parent=0&exclude=233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,212,214,47,35,125,6,209,252,96,3,5' ) ) : // to make it simple I use default categories
                echo '<select name="categoryfilter6"><option>Select Language...</option>';
                foreach ( $terms as $term ) :
                    echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
                endforeach;
                echo '</select>';
            endif;
        ?>

        <button>Apply filter</button>
        <input type="hidden" name="action" value="mylearningfilter">

    </form>

        <?php

$args = array(
    'orderby' => 'date',
    'post_type' => 'post',
    'post_status' => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page' => 18,
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array('tutorials')
        ),
    )
);

$l_query = new WP_Query( $args );


global $wp_query;

        if ( $l_query->have_posts() ) : ?>
                <div id="responsesss">
            <?php

            $archive_content_layout = get_option( 'archive_content_layout', 'th-grid-2' );
            echo '<div class="posts-wrap ' . esc_attr( $archive_content_layout ) . '">';

                /* Start the Loop */
                while ( $l_query->have_posts() ) : $l_query->the_post();

                    get_template_part( 'template-parts/content', get_post_format() );

                endwhile;

            echo '</div><!-- .posts-wrap -->';
        wp_reset_postdata();
?>
</div>
<?php 

if (  $l_query->max_num_pages > 1 ) :
    echo '<div id="misha_learning_loadmore">More posts</div>'; // you can use <a> as well
endif;
            the_posts_pagination();

        else :

            get_template_part( 'template-parts/content', 'none' );

        endif; ?>

myloadmorelearning.js

jQuery(document).ready( function($) {

    /*
     * Load More
     */
    $('#misha_learning_loadmore').click(function(){

        $.ajax({
            url : misha_loadmore_learning_params.ajaxurl, // AJAX handler
            data : {
                'action': 'loadmorelearningbutton', // the parameter for admin-ajax.php
                'query': misha_loadmore_learning_params.posts, // loop parameters passed by wp_localize_script()
                'page' : misha_loadmore_learning_params.current_page // current page
            },
            type : 'POST',
            beforeSend : function ( xhr ) {
                $('#misha_learning_loadmore').text('Loading...'); // some type of preloader
            },
            success : function( data ){
                if( data ) {

                    $('#misha_learning_loadmore').text( 'More posts' );
                    $('#responsesss').append(data); // insert new posts
                    misha_loadmore_learning_params.current_page++;

                    if ( misha_loadmore_learning_params.current_page == misha_loadmore_learning_params.max_page ) 
                        $('#misha_learning_loadmore').hide(); // if last page, HIDE the button

                } else {
                    $('#misha_learning_loadmore').hide(); // if no data, HIDE the button as well
                }
            }
        });
        return false;
    });

    /*
     * Filter
     */
    $('#misha_learning_filters').submit(function(){

        $.ajax({
            url : misha_loadmore_learning_params.ajaxurl,
            data : $('#misha_learning_filters').serialize(), // form data
            dataType : 'json', // this data type allows us to receive objects from the server
            type : 'POST',
            beforeSend : function(xhr){
                $('#misha_learning_filters').find('button').text('Filtering...');
            },
            success:function(data){

                $('html,body').animate({ scrollTop: $("#main").offset().top }, "slow");

                // when filter applied:
                // set the current page to 1
                misha_loadmore_learning_params.current_page = 1;

                // set the new query parameters
                misha_loadmore_learning_params.posts = data.posts;

                // set the new max page parameter
                misha_loadmore_learning_params.max_page = data.max_page;

                // change the button label back
                $('#misha_learning_filters').find('button').text('Apply filter');

                // insert the posts to the container
                $('#responsesss').html(data.content);

                // hide load more button, if there are not enough posts for the second page
                if ( data.max_page < 2 ) {
                    $('#misha_learning_loadmore').hide();
                } else {
                    $('#misha_learning_loadmore').show();
                }
            }
        });

        // do not submit the form
        return false;

    });

});

0 个答案:

没有答案