Site icon Hip-Hop Website Design and Development

Utilizing Ajax and WP_Query to load extra posts on class web page fails

I am utilizing the code under to load extra articles on click on beneath the ten posts which might be already loaded on web page load.
It really works completely on my entrance web page, however once I apply it to my class web page, it is going to present all posts as a substitute of simply those throughout the class.
For instance: "domain/category/testcategory/"
I attempted including this to the capabilities.php half the place $args is specified:

        'class'          => get_queried_object_id(),
        'category__in'      => array( get_queried_object_id() ),

No luck although. It truly broke the entire thing once I added it. It stopped loading extra posts on click on.
Can somebody inform me what I am lacking? I simply began studying WP_Query and Ajax and I have been attempting to get this sorted for a few days now. Any recommendation could be appreciated.
That is the complete code I am utilizing:

// capabilities.php

operate mytheme_scripts() {
 wp_register_script( 'core-js', get_template_directory_uri() .'/loadmore.js', array('jquery'), '', true );
 wp_enqueue_script( 'core-js' );
wp_localize_script( 'core-js', 'ajax_posts', array(
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'noposts' => __('No older posts discovered', 'ths-theme'),
)); 
}
add_action( 'wp_enqueue_scripts', 'mytheme_scripts' );

operate more_post_ajax(){

    $ppp = (isset($_POST["ppp"])) ? $_POST["ppp"] : 3;
    $web page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;



    header("Content-Type: text/html");

    $queried_object = get_queried_object_id();  
    $cat_id = $queried_object->term_id;
    $args = array(      
        'offset' => '1',
        'suppress_filters' => true,
        'post_type' => 'put up',
        'posts_per_page' => $ppp,
        'paged'    => $web page,
        'class'      => array( get_queried_object_id() ),

    );


    if ( isset( $args['offset'] ) ) {
        $offset = absint( $args['offset'] );
        $args['offset'] = absint( ( $args['paged'] - 1 ) * $args['posts_per_page'] ) + $offset;
    }

    $class = get_the_category();
    $mother or father = get_cat_name( $class[1]->mother or father );
    $little one = get_cat_name($class);
    $parentlink = get_category_link( $class[1]->mother or father );

    $loop = new WP_Query($args);

    $out = '';

    if ($loop -> have_posts()) :  whereas ($loop -> have_posts()) : $loop -> the_post();
    $class = get_the_category();
    $mother or father = get_cat_name( $class[1]->mother or father );
    $little one = get_cat_name($class);
    $parentlink = get_category_link( $class[1]->mother or father );
        $out .= ' <div class="masonry-item"><div class="masonry-content"><div class="i-container">
        <a href=" '.get_the_permalink().' ">'.get_the_post_thumbnail( get_the_ID(), $dimension = 'latest-front' , $attr ).'</a>

                <div class="featured-cat f-c-latest"><a href=" ' . $parentlink .' ">
                '.$mother or father.'
                </a></div></div>
                <a href=" '.get_the_permalink().' ">
                <h2>'.get_the_title().'</h2></a>
                
                <div class="latestda">
                <span class="date">'.get_the_date( 'dS M Y' ).' </span>
                <span class="bold">COMMENTS '.get_comments_number().'</span></div>
         </div></div> ';

    endwhile;
    endif;
    wp_reset_postdata();
    die($out);
}

add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
 ?>
//class.php

                    <div class="latest-block">
                <header class="archive-header">
                <h1 class="archive-title"><?php single_cat_title( '', true ); ?></h1>
                </header>
                    <div  id="ajax-posts" class="masonry">
                        <?php
                            $postsPerPage = 10;
                            $args = array(
                            'class'          => get_queried_object_id(),
                            'category__in'      => array( get_queried_object_id() ),
                            'post_type' => 'put up',
                            'posts_per_page' => $postsPerPage,
                            );
                        $loop = new WP_Query($args);
                        whereas ($loop->have_posts()) : $loop->the_post();
                        ?>
 

                        <div class="masonry-item">
                            <div class="masonry-content">
                                <div class="i-container">
                                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                    <?php the_post_thumbnail( 'latest-front' );?>
                                    </a>
                                    <?php    $class = get_the_category();
                                    $mother or father = get_cat_name( $class[1]->mother or father );
                                    $little one = get_cat_name($class);
                                    $parentlink = get_category_link( $class[1]->mother or father );
                                    if ( ! empty( $mother or father ) ) {
                                    echo '<div class="featured-cat f-c-latest"><a href=" ' . $parentlink .' ">';
                                    echo $mother or father;
                                    echo '</a></div>';
                                    }
                                    else {
                                    echo $little one; 
                                    }
                                    ?>
                                </div>
                                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                                <h2><?php the_title(); ?></h2>
                                </a>
                                <?php
                                $articleda = get_the_date( 'dS M Y' );
                                echo '<div class="latestda">';
                                echo '<span class="date">';
                                echo $articleda;
                                echo '</span>';
                                echo ' <span class="bold">COMMENTS ';
                                echo get_comments_number();
                                echo '</span></div>';
                                ?> 
                              
                            </div>
                        </div>  <?php
                                    endwhile;
                                    wp_reset_postdata();
                                ?>
                    </div>
                    <div id="more_posts" class="misha_loadmore">Load Extra</div>
// loadmore.js

var ppp = 10; // Submit per web page
var pageNumber = 1;


operate load_posts(){
    pageNumber++;
    var str = '&pageNumber=' + pageNumber + '&ppp=' + ppp + '&motion=more_post_ajax';
    jQuery.ajax({
        kind: "POST",
        dataType: "html",
        url: ajax_posts.ajaxurl,
        knowledge: str,
        success: operate(knowledge){
            var jQuerydata = jQuery(knowledge);
            if(jQuerydata.size){
                jQuery("#ajax-posts").append(jQuerydata);
                jQuery("#more_posts").attr("disabled",false);
            } else{
                jQuery("#more_posts").attr("disabled",true);
            }
        },
        error : operate(jqXHR, textStatus, errorThrown) {
            jQueryloader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
        }

    });
    return false;
}

jQuery("#more_posts").on("click",operate(){ // When btn is pressed.
    jQuery("#more_posts").attr("disabled",true); // Disable the button, temp.
    load_posts();
    jQuery(this).insertAfter('#ajax-posts'); // Transfer the 'Load Extra' button to the tip of the the newly added posts.
});