如何获得总帖子页数?

时间:2015-05-01 08:51:06

标签: javascript php jquery ajax wordpress

您好我正在为我的自定义帖子类型进行无限滚动,我不知道如何获取max_num_pages并将其传输到javascript。这是我的无限滚动调用:

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();
                ?>

如何获取帖子的总页数,然后将其转移到javascript以测试是否有更多帖子需要加载?

1 个答案:

答案 0 :(得分:1)

为什么不想使用内置的WordPress AJAX支持?
你可以添加两个动作处理程序,如:

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

您可以在此处查看文档:
http://codex.wordpress.org/AJAX_in_Plugins

如果您想将某些值传递给JavaScript,可以使用I18n - https://codex.wordpress.org/I18n_for_WordPress_Developers#Handling_JavaScript_files