AJAX 加载更多按钮 returns 每次都发布相同的帖子

AJAX load more button returns the same posts every time

AJAX 每次加载更多 returns 相同的帖子。问题在 WP_Query 中,因为此代码在我删除它时有效。我无法弄清楚我的 WP_Query.

有什么问题

这是 index.php

的一部分
$category_id = get_cat_ID('Događaji');
        $exlude_latest_featured_post = array($featuredID);
        $args = array(
            'category__not_in' => array($category_id),
            'post__not_in' => $exlude_latest_featured_post,
            'posts_per_page' => 10,
        );

        $main_loop = new WP_Query ( $args );
        while ($main_loop->have_posts()) : $main_loop->the_post(); ?>

        <?php get_template_part('loop/content'); ?>

        <?php endwhile; ?>

        <div class="listing-more-posts">
            <a>
               <?php
                   $link_label = '<img class="listing-more-posts-image" src="'. get_bloginfo('template_directory') .'/images/more-posts-2.gif" alt="Učitaj još postova">';
                   next_posts_link($link_label);
               ?>
            </a>
        </div>

这是我的 loadmore.js 文件。正如我所说,当我在 index.php

中删除 WP_Query 时它工作正常
jQuery(function($){

// Ajax (next) page load
    var pageLoading = false;
    function loadContent($link){

        if( pageLoading == true ){
            return;
        }

        pageLoading = true;

        $.ajax({
            type: "get",
            url: $link.attr("href"),
            dataType: "html",
            success: function(response){
                var html = $('<div>').html(response);

                // 'standard' blog layout
                var $articles = $(html).find("#post-listing article");

                //$("#main").append(articles);
                $link.closest(".listing-more-posts").before($articles);

                // remove old nav wrapper (new one is added with new content)
                // if( $link != null ){
                $link.closest(".listing-more-posts").html($(html).find(".listing-more-posts").html());
                // }

                pageLoading = false;
            }
        });
    }

      $("body").on("click", ".listing-more-posts a", function(e){
        e.preventDefault();
        console.log("click");

        // var $link = $(this);
        $(this).parent().addClass('loading');
        loadContent($(this));
    });  
});

我正在寻找答案,但找不到。从我目前找到的答案来看,我认为我需要对跟踪页码做一些事情,但我不确定该怎么做。

谢谢

我知道了。

我通过从上方执行操作来破坏主循环。我继续搜索,一段时间后我发现了一个非常有用的动作挂钩,叫做 per_get_posts,它是我的问题的解决方案

This hook is called after the query variable object is created, but before the actual query is run.
- Wordpress Codex

我现在要做的就是这两件事:

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $category_id = get_cat_ID('Događaji');
            $query->set( 'category__not_in', array($category_id) );
        }
    }
add_action( 'pre_get_posts', 'exclude_category' );
//Exclude latest featured post from showing in loop
function exclude_latest_post( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $exlude_latest_featured_post = array($featuredID);
            $query->set( 'post__not_in', $exlude_latest_featured_post );
        }
    }
add_action( 'pre_get_posts', 'exclude_latest_post' );