@antowa_plawkevich
Junior Front-end developer

Ajax подгрузка постов по клику?

При клике на пагинацию должен меняться пост аяксом, но в массиве у меня выводятся все посты, а не постранично.

Custom-template.php
<section class="section ps2">
        <div class="s-wrap">
            <div class="conteiner">
                <div class="s-title">text</div>
                <p>text</p>

                <div class="double-tabs">
                    <ul class="change-tabs">
                        <?php
                        $categoryLists = get_field('category__list');
                        if($categoryLists):
                        foreach ($categoryLists as $categoryList):
                            ?>
                            <li class=""><?php echo $categoryList->name?></li>
                        <?php endforeach;
                        endif;
                        ?>
                    </ul>

                    <div class="tabs active">
                        <ul class="tabs__caption">
                        <? foreach ($categoryLists as $categoryList):
                            $subCategories = get_categories([
                                'parent' => $categoryList->term_id,
                                'hide_empty' => false
                            ]);
                            foreach ($subCategories as $subCategory):?>
                                <li><?= $subCategory->name?></li>
                         <?php endforeach;?>

                            <?php endforeach;?>
                        </ul>
                        <?php
                        foreach ($categoryLists as $categoryList):
                        $subCategories = get_categories([
                            'parent' => $categoryList->term_id,
                            'hide_empty' => false
                        ]);
                        foreach ($subCategories as $subCategory) :
                           global $news_id;
                            $news_id = $subCategory->term_id;
                            $catPosts = array(
                                'cat'=> $news_id,
                                'posts_per_page' => 1,
                                'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
                            );
                            $childPost = new WP_Query($catPosts);
                            ?>
                            <div class="tabs__content active">
                                <div class="news">
                            <?php if($childPost->have_posts()):
                                while ( $childPost->have_posts() ) : $childPost->the_post();?>

                                    <a href="" style="background-image: url('img')">
                                        <p><?php the_title();?></p>
                                    </a>
                            <?php endwhile;
                            endif;
                            wp_reset_postdata();
                            ?>
                                    <nav class="pagination" style="display: flex">
                                        <?php
                                        $big = 999999999;
                                        echo paginate_links( array(
                                            'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                                            'format' => '?paged=%#%',
                                            'current' => max( 1, get_query_var('paged') ),
                                            'total' => $childPost->max_num_pages,
                                            'prev_text' => '&laquo;',
                                            'next_text' => '&raquo;'
                                        ) );
                                        ?>
                                    </nav>
                                    <?php wp_reset_postdata(); ?>
                                </div>
                            </div>
                        <?php endforeach;
                        endforeach;
                        ?>
                    </div>
                </div>

            </div>
        </div>
    </section>


Ajax:
$(document).on( 'click', '.pagination a.page-numbers', function( event ) {
        event.preventDefault();

        var ajaxurl = "<?php echo admin_url('admin-ajax.php')?>";

        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                'action': 'my_ajax_action',
                'post_id': <?php echo $news_id; ?>

            },
            success : function(data) {
                $('html').append(data);
                console.log(data);
            }
        });
    });


Functions.php:

add_action('wp_ajax_my_ajax_action', 'my_ajax_action' );
add_action('wp_ajax_nopriv_my_ajax_action', 'my_ajax_action' );
function my_ajax_action() {
    $args = $_POST['post_id'];

    $my_posts = array(
        'cat'=> $args,
        'posts_per_page' => 1,
        'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    );


    $loop = new WP_Query( $my_posts );?>
        <?php if($loop->have_posts()):
        $data = [];
        while ( $loop->have_posts() ) : $loop->the_post();
        $data = the_title();
        ?>
        <?php endwhile;
        wp_send_json_success( $data );
        endif;
        die;
}
  • Вопрос задан
  • 238 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Frayl
Что за : заместо {}, это по твоему питон или чё?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы