@antowa_plawkevich
Junior Front-end developer

WP Ajax Cutom post types?

На сайте созланые кастомные посты, вывожу инфу через acf-repeator и пытаюсь сделать пагинацию данных колонок через ajax, при клике на пагинацию в консоль отдаёт false, не понимаю в чем проблема.

Custom-template:
if( ('page') ) {
        $page = get_query_var( 'page', 1);
    } else {
        $page = 1;
    }
    if($page < 2) {
        $page = 1;
    }
    // Variables
    $row              = 0;
    $posts_per_page   = 3; // How many images to display on each page
    $content          = get_field( 'image_gallery' );
    $total            = count( $content );
    $pages            = ceil( $total / $posts_per_page );
    $min              = ( ( $page * $posts_per_page ) - $posts_per_page ) + 1;
    $max              = ( $min + $posts_per_page ) - 1;
    ?>
    <?php if( have_rows('image_gallery') ): ?>
        <div class="container-propositions" style="display: flex">
            <?php while( have_rows('image_gallery') ): the_row();
                $row++;
                // Ignore this row if $row is lower than $min
                if($row < $min) { continue; }

                // Stop loop completely if $row is higher than $max
                if($row > $max) { break; }
                $sub_field = get_sub_field('image');
                ?>
                <img src="<?php echo $sub_field; ?>" alt="" style="max-width: 30%">
            <?php endwhile; ?>
        </div>
        <div class="pagination" style="display: flex; justify-content: center">
            <?php
            // Pagination
            echo paginate_links( array(
                'base' => get_permalink() . '%#%' . '/',
                'format' => '?page=%#%',
                'current' => $page,
                'total' => $pages,
                'type' => 'plain'
            ) );
            ?>
        </div>
    <?php endif; ?>

<script>
    $(document).on( 'click', 'div.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 get_the_ID($the_query); ?>

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

Functions.php:
add_action('wp_ajax_nopriv_my_ajax_action', 'my_ajax_action' );
add_action('wp_ajax_my_ajax_action', 'my_ajax_action' );
function my_ajax_action() {
    $post_id = $_POST['post_id'];
    if( have_rows('image_gallery', $post_id) ):
        $data = [];
        // loop through the rows of data
        while ( have_rows('image_gallery', $post_id) ) : the_row();

            // display a sub field value
            $data = get_sub_field('image');

        endwhile;
        wp_send_json_success( $data );

    else :
        wp_send_json_error( 'test' );

    endif;
}
  • Вопрос задан
  • 32 просмотра
Пригласить эксперта
Ваш ответ на вопрос

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

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