Loading articles on click AJAX in Single /categorry child template

enter image description here

i want to create loading more article on click in Single Template Post and Child of category.
At first i just want to say my code work in tempalte page and parent categorry.
The problem is: after click button querry return the same date. Like on this screan:

to loading on click i using Ajax Pagination and Infinite Scroll
code in template:

                          //'page' doasnt work too
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$tag = get_field('tag_na_dol_strony');  

/*
TEST 
$args = array(
    'posts_per_page' => 4,
    'post__not_in' => array(get_the_ID()),
    'tag' => 'featured',
    'paged' => $paged,
);

*/
$args = array(
    'posts_per_page' => 4,
    'post__not_in' => array(get_the_ID()),
    'orderby' => 'date',
    'order' => 'DESC',
    'tax_query' => array(
        array(
            'taxonomy' => 'marka',
            'field' => 'ids',
            'terms' => $tag->term_id
        )
    )
);



//query_posts($args);
$query = new WP_Query( $args );
?>

    <section class="posts pt-6 pb-3 mb-4 border-top border-bottom border-primary-light">
        <div class="container">
            <h2 class="section-subtitle mb-6"><?php echo $tag ->name; ?> na Ładnebebe</h2>
            <div class="row posts-grid ajax-posts pt-3 mb-5">
                   <?php        

                  if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
                //content
                    endwhile;
                    wp_reset_postdata();
                    endif;?>
            </div> 
    <div class="text-center">
        <div class="navigation text-c">
            <div class="nav-links">
                <?php
                echo paginate_links( array(
                    'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
                    'total'        => $query->max_num_pages,
                    'current'      => max( 1, get_query_var( 'paged' ) ),
                    'format'       => '?paged=%#%',
                    'show_all'     => false,
                    'type'         => 'plain',
                    'end_size'     => 2,
                    'mid_size'     => 1,
                    'prev_next'    => true,
                    'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
                    'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
                    'add_args'     => false,
                    'add_fragment' => '',
                    'before_page_number' => '<span class="page-link">',
                    'after_page_number' => '</span>',
                ) );

                ?>
            </div>
        </div>
    </div>

        </div>
    </section>

Code in functions.php:

function custom_nav_class($classes, $item){
$itemObjectID = $item->object_id;
$itemObject = $item -> object;
if ( $itemObject == 'category') :
    $classes[] = get_category($itemObjectID) -> slug;
endif;

return $classes;
}
add_filter('nav_menu_css_class' , 'custom_nav_class' , 10 , 2);
function alter_query($query) {
//gets the global query var object
global $wp_query;

//gets the front page id set in options

if ( !$query->is_main_query() ){


     return;
}


if ( !is_category() )
    return;

$category = get_queried_object();

$parent_id = $category->category_parent;

if($parent_id==11 || $category->term_id ==11) {
    $query->set('posts_per_page',14);
    return;
} elseif($parent_id==5140 || $category->term_id ==5140) {
    $query->set('posts_per_page',10);
}


//First, define your desired offset...
$offset = 2;

//Next, determine how many posts per page you want (we'll use WordPress's settings)
$ppp = get_option('posts_per_page');

//Next, detect and handle pagination...
if ( $query->is_paged ) {

    //Manually determine page query offset (offset + current page (minus one) x posts per page)
    $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );

    //Apply adjust page offset
    $query->set('offset', $page_offset );

}
else {

    //This is the first page. Just use the offset...
    $query->set('offset',$offset);

}


//we remove the actions hooked on the '__after_loop' (post navigation)
remove_all_actions ( '__after_loop');
   }

   add_action('pre_get_posts','alter_query');

  function new_subcategory_hierarchy() {
$category = get_queried_object();

$parent_id = $category->category_parent;

$templates = array();

if ( $parent_id == 0 ) {
    // Use default values from get_category_template()
    $templates[] = "category-{$category->slug}.php";
    $templates[] = "category-{$category->term_id}.php";
    $templates[] = 'category.php';
} else {
    // Create replacement $templates array
    $parent = get_category( $parent_id );

    // Current first
    $templates[] = "category-{$category->slug}.php";
    $templates[] = "category-{$category->term_id}.php";

    // Parent second
    $templates[] = "category-{$parent->slug}.php";
    $templates[] = "category-{$parent->term_id}.php";
    $templates[] = 'category.php';
}
return locate_template( $templates );
}

add_filter( 'category_template', 'new_subcategory_hierarchy' );

Does anyone know where i can make a mistake ? its realy intresting becouse this code work in page template and parent categorry. In my opinion the problem can be in functions.php in alter_query or custom_nav_class

Read more here:: Loading articles on click AJAX in Single /categorry child template

Leave a Reply

Your email address will not be published. Required fields are marked *