0

I have a function which loads in posts by click, however on category pages /category/**** when using that button it should only load posts for the same category.

I can't seem to get it to work and not sure what im doing wrong.

i've understood i need to pass the current category to load more buttons. Ive done that by adding:

data-category="<?php echo $category[0]->cat_ID; ?>"

to the button.

My Function is as follows:

function more_posts()
{

    $offset = (int) $_POST['offset'];
    $data = new WP_Query([
        'post_status' => 'publish',
        'post_type' => 'post',
        'posts_per_page' => 6, // 10 by default
        'offset' => $offset, // 10 by default
        'cat' =>  array($_POST['cat_id']),
    ]);
    
    $posts = $data->posts;

    ob_start();
    foreach ($posts as $entry) :
        $image       = wp_get_attachment_image_src(get_post_thumbnail_id($entry->ID), 'medium');
        $categories  = get_the_category($entry->ID);
        $blurb_title = get_field('blog_title_blurb', $entry->ID);
    

?>

and the ajax js is:

bindLoadMore() {
    $(".js-load-more-posts").on("click", function (e) {
      let button = $(this);
      let offset = button.attr("data-offset");
      button.addClass("hidden");
      $(".loading").removeClass("hidden");
      $.ajax({
        method: "POST",
        url: woocommerce_params.ajax_url,
        data: { action: "more_posts", offset: offset },
        success: function (response) {
          $(".loading").addClass("hidden");
          button.removeClass("hidden");
          $(".blog-entries").append(response.data.html);
          button.attr("data-offset", parseInt(offset) + response.data.posts);
          if (response.data.posts === 0) {
            $(".load-more-posts-container .button").attr("disabled", 'disabled');
          }
        }
      });
    }

cant make it work, any push in the right direction?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.