weixin_33701251 2015-05-01 08:51 采纳率: 0%
浏览 14

如何获得帖子总数?

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?

  • 写回答

1条回答 默认 最新

  • 衫裤跑路 2015-05-01 11:44
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了