dongxing6802 2014-07-27 17:46
浏览 66

AJAX脚本在WordPress的单个类别视图中加载所有类别的帖子

I have a function that dynamicaly loads more posts after clicking the button and a LoopHandler.php that is displaying posts. Everything works perfect, apart from that the loop is always displaying all of the posts. So if I am on the category page it displays posts from all of the categories. Why is it happening so? How to fix it?

// ajaxLoop.js
jQuery(function($){
    var page = 1;
    var loading = true;
    var $window = $(window);
    var $content = $("body #post-stream .block");
    var load_posts = function(posts, row){
            $.ajax({
                type       : "GET",
                data       : {numPosts : posts, pageNumber: page, postsInRow: row},
                dataType   : "html",
                url        : "../loopHandler.php",
                beforeSend : function(){
                    $content.append('<div id="temp_load" style="text-align:center; clear: both; padding: 30px 0;">\
                        <img src="http://cayennestudio.pl/segritta/wp-content/themes/segritta-ajax/css/img/ajax-loader.gif" />\
                        </div>');
                },
                success    : function(data){
                    $data = $(data);
                    $(window).scrollTop($(window).scrollTop()-1);
                    if($data.length){
                        $data.hide();
                        $content.append($data);
                        $data.fadeIn(500, function(){
                            $("#temp_load").remove();
                            loading = false;
                        });
                    } else {
                        $("#temp_load").remove();
                    }
                },
                error     : function(jqXHR, textStatus, errorThrown) {
                    $("#temp_load").remove();
                    alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                }
        });
        page++;
    }
    var load_szorts = function(posts, row){
            $.ajax({
                type       : "GET",
                data       : {numPosts : posts, pageNumber: page, postsInRow: row},
                dataType   : "html",
                url        : "http://cayennestudio.pl/segritta/wp-content/themes/segritta-ajax/loopHandlerSzorts.php",
                beforeSend : function(){
                },
                success    : function(data){
                    $data = $(data);
                    $data.hide();
                    $content.append($data);
                    $data.fadeIn(500, function(){
                        loading = false;
                    });
                },
                error     : function(jqXHR, textStatus, errorThrown) {
                    alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
                }
        });
        page++;
    }
    if ( $('body.home').length ) {
        load_posts(4, 2);
        $('.load-more').click(function(){
            load_posts(4, 2);
        }); 
    };
    if ( $('body.category').length ) {
        load_posts(9, 3);
        $('.load-more').click(function(){
            load_posts(9, 3);
        });
    };
    if ( $('body.post-type-archive-szort').length ) {
        load_szorts(8, 4);
        $('.load-more').click(function(){
            load_szorts(4, 4);
        });
    };
});

LoopHandler.php

<?php
// Our include
    define('WP_USE_THEMES', false);
    require_once('../../../wp-load.php');

// Our variables
    $numPosts = (isset($_GET['numPosts'])) ? $_GET['numPosts'] : 0;
    $page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0;

    query_posts(array(
           'posts_per_page' => $numPosts,
           'paged'          => $page,
    ));

    global $var_counter;
    global $var_row;
    $var_counter = 0;
    $var_row = (isset($_GET['postsInRow'])) ? $_GET['postsInRow'] : 2;

// our loop
    if ( have_posts() ) :

        while ( have_posts() ) : the_post();

            get_template_part( 'content', get_post_format() );

        endwhile;

    else:

        get_template_part( 'content', 'none' );

    endif;

    wp_reset_query();
?>
  • 写回答

1条回答 默认 最新

  • doujia1939 2014-07-27 18:10
    关注

    First of all it should go to WordPress.StackExchange, I guess.

    And going back to your problem...

    First problem: Bad AJAX handling

    You shouldn't create your own PHP files to process AJAX requests in WordPress. WP has it's own mechanisms to process AJAX requests and you should use them for compatibility reasons.

    You can read more on this topic here: http://codex.wordpress.org/AJAX_in_Plugins

    You just need to create a filter with correct name and call AJAX requests with correct action value...

    Second Problem: Always all posts

    You don't pass current category anywhere in your AJAX request and you don't put it in your WP_Query.

    You should pass current category ID in your AJAX request like so:

    $.ajax({
        type       : "GET",
        data       : {numPosts : posts, pageNumber: page, postsInRow: row, currentCat: currentCat}, // of course you need to make sure that currentCat variable contains the ID of current category or 0 otherwise
        dataType   : "html",
        ...
    

    And then process it in your PHP code like so:

    $args = array(
           'posts_per_page' => $numPosts,
           'paged'          => $page,
    );
    if ( isset($_GET['currentCat']) && $_GET['currentCat'] ) {
        $args['cat'] = $_GET['currentCat'];
    }
    query_posts($args);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题