简体   繁体   English

AJAX加载更多功能是不是老数据成倍增加?

[英]AJAX load more function multiplies the old data?

I have a PHP code that creates a JSON data from wordpress posts in the database.我有一个 PHP 代码,它从数据库中的 wordpress 帖子创建一个 JSON 数据。

I load this JSON on my html page using AJAX.我使用 AJAX 在我的 html 页面上加载这个 JSON。

The above works fine.以上工作正常。

Now I need to add a "Load More" button so everytime its pressed, we load another 10 posts and append/add them to the ones that were already loaded WITHOUT having to delete the old ones and re-adding them.现在我需要添加一个“加载更多”按钮,所以每次按下它时,我们加载另外 10 个帖子并将它们附加/添加到已经加载的帖子中,而不必删除旧帖子并重新添加它们。

So this my AJAX code for loading more:所以这是我用于加载更多内容的 AJAX 代码:

var i = 0;
$(document).on("click", ".loadMoreBtn", function () {
    $.ajax({
        url: 'https://some-domain.com/index.php?t=' + mainCat + '&page=' + i + '',
        dataType: 'json',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function (data, status) {

            if (!$.trim(data)) {

            }
            else {
            }

            $.each(data, function (pi, item) {

                var id = item.id;
                var img = item.img;
                var title = item.title;
                var date_added = item.date_added;

                var item = '' + title + '';

                $('.myDiv').before(item);
                i++;
            });

        },

        error: function () {
            //error handling////
        }
    });
});

And this is my PHP code:这是我的 PHP 代码:

<?php
    header('Access-Control-Allow-Origin: *');
    header('Content-Type: application/json');
    $path = $_SERVER['DOCUMENT_ROOT'];
    include_once $path . '/wp-config.php';
    include_once $path . '/wp-load.php';
    include_once $path . '/wp-includes/wp-db.php';
    include_once $path . '/wp-includes/pluggable.php';

      $t = $_GET['t'];
      $page = $_GET['page'];
      $posts = get_posts(array(
      'posts_per_page' => $page, //add -1 if you want to show all posts
      'post_type' => 'post',
      'post_status' => 'publish',
      'tax_query' => array(
                  array(
                        'taxonomy' => 'category',
                        'field' => 'slug',
                        'terms' => $t //pass your term name here
                          )
                        ))
                       );



    $output= array();
    foreach( $posts as $post ) {

        $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

        $mysqldate = $post->post_date;
        $phpdate = strtotime( $mysqldate );
    $mysqldate = date( 'F d Y', $phpdate );
        // Pluck the id and title attributes
        $output[] = array( 'id' => $post->ID, 'title' => $post->post_title, 'date_added' => $mysqldate, 'img' =>$feat_image );
    }

    echo json_encode( $output );

When I click on the ' Load More ' button, it acts strangely!当我点击“ Load More ”按钮时,它的行为很奇怪! it basiclaly adds the old data and multiplies the same ones and adds/loads some new ones as well.它基本上添加旧数据并乘以相同的数据并添加/加载一些新数据。

I know I am missing something in my PHP code but I couldn't figure out what.我知道我的 PHP 代码中遗漏了一些东西,但我不知道是什么。

Could someone please advice on this issue?有人可以就这个问题提出建议吗?

Thanks in advance.提前致谢。

The error is in your wordpress query.错误在您的 wordpress 查询中。 "posts_per_page" set how many posts will be loaded. “posts_per_page”设置将加载多少帖子。 Set that as how many post should be loaded like 12 or something.将其设置为应加载多少帖子,例如 12 或其他内容。

The parameter your want to set as your $page parameter is "paged".您要设置为 $page 参数的参数是“paged”。 Eg.例如。 $query = new WP_Query( array( 'paged' => 6 ) ); // page number 6

https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

您也可以使用 WP API 而不是自己滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM