Zobo
Zobo

Reputation: 293

How to get total post pages?

Hello I'm making infinite scroll for my custom post type and I dont know how to get max_num_pages and transfer it to javascript. Here is my infinite scroll call:

containerItemSelector = ".home-row";
    counter = 1;
    pageNumber = 1;
    load = true;
    load2 = true;
    loadPosts = function() {
        $.ajax({
            url: templateURL + '/js/load_post.php',
            type: 'GET',
            dataType: 'html',
            data: {
                posts : countPosts,
                page : pageNumber
            },
            beforeSend: function() {
                //$( '#sliders' ).after('<span id="loading"><img src="' + templateURL + '/images/loader.GIF"></span>');
                load = false;
            },
            success: function(data) {
                var item = $(data);
                item.each(function(i) {
                  $(containerItemSelector).append($(this));
                  var slider = $(this).find('.slider-left').attr('id');
                  jssor_slider_left.push(new $JssorSlider$(slider, options1));
                  var total = jssor_slider_left.length - 1;
                  var parentWidth = jssor_slider_left[total].$Elmt.parentNode.clientWidth;

                    if (parentWidth)
                        jssor_slider_left[total].$ScaleWidth(Math.min(parentWidth,954));
                    else
                        window.setTimeout(scales, 30);
                    $(this).find('.img-wrap2').fadeOut(1500, function(){
                        $(this).remove();
                    });
                    if(counter % 2 == 0){
                        $(this).closest('.right-col').after("<div class='clearfix'> </div>");

                }
                counter++;
            });
                $( '#loading' ).remove();
                load = true;
            },
            error: function(result) {
            }
        });
    };

load_post.php:

<?php

define('WP_USE_THEMES', false);
require_once('../../../../wp-load.php');


if (isset($_GET['posts'])) {
    $posts = $_GET['posts'];
}

if ( isset($_GET['page']) ) {
    $page = $_GET['page'];
}
$the_query = new WP_Query( array('post_status'=>'publish', 'post_type' => 'architektura', 'orderby' => 'date', 'order' => 'DESC', 'paged'=>$page) );

 while ( $the_query->have_posts() ) : $the_query->the_post();
     get_template_part('content', 'architektura');
endwhile;

wp_reset_query();
                ?>

How can I get total pages of my posts and then transfer it to javascript to test if there are more posts to load?

Upvotes: 0

Views: 97

Answers (1)

Lukas Pawlik
Lukas Pawlik

Reputation: 146

Why don't you want to use builtin WordPress AJAX support?
You could add two action handlers like :

add_action( 'wp_ajax_my_action', 'my_action_method' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action_method' );

You can check the documentation here :
http://codex.wordpress.org/AJAX_in_Plugins

If you want to pass some values to JavaScript you can use I18n - https://codex.wordpress.org/I18n_for_WordPress_Developers#Handling_JavaScript_files

Upvotes: 1

Related Questions