V
V
vladzvezdin2018-09-21 10:28:43
WordPress
vladzvezdin, 2018-09-21 10:28:43

Ajax loading wp post types?

There are two pages on the site, for two corresponding custom post types (Catalogs and Videos). On each page, ajax loading of posts by clicking on a button should be implemented. For one page it is implemented without problems. But the same approach does not work for the second one.

//AJAX
function true_load_posts_catalogs(){
    $args = unserialize(stripslashes($_POST['query']));
    $args['paged'] = $_POST['page'] + 1; // следующая страница
    $args['post_status'] = 'publish';
    $q = new WP_Query($args);
    if( $q->have_posts() ):
        while($q->have_posts()): $q->the_post();
            /*
             * Со строчки 13 по 27 идет HTML шаблон поста, максимально приближенный к теме TwentyTen.
             * Для своей темы вы конечно же можете использовать другой код HTML.
             */
            ?>
            <a href="<?php the_field('catalog-file'); ?>" class="catalog-item">
                <div class="catalog-item-img">
                    <?php the_post_thumbnail('full'); ?>
                </div>

                <div class="catalog-item-text">
                    <p><?php the_title(); ?></p>
                    <p><?php the_field('catalog-part');?></p>
                    <p><?php the_field('catalog-title');?></p>
                </div>
            </a>
        <?php
        endwhile;
    endif;
    wp_reset_postdata();
    die();
}


add_action('wp_ajax_loadmore', 'true_load_posts_catalogs');
add_action('wp_ajax_nopriv_loadmore', 'true_load_posts_catalogs');




function true_load_posts_videos(){
    $args = unserialize(stripslashes($_POST['query']));
    $args['paged'] = $_POST['page'] + 1; // следующая страница
    $args['post_status'] = 'publish';
    $q = new WP_Query($args);
    if( $q->have_posts() ):
        while($q->have_posts()): $q->the_post();
            /*
             * Со строчки 13 по 27 идет HTML шаблон поста, максимально приближенный к теме TwentyTen.
             * Для своей темы вы конечно же можете использовать другой код HTML.
             */
            ?>
            <div class="video-item disable-video-item">
                <div class="video-item-img">
                    <?php the_post_thumbnail('full'); ?>
                </div>
                <div class="video-item-description">
                    <h3><?php the_title();?></h3>
                    <h4><?php the_field('video-subtitle');?></h4>
                    <a href="<?php the_field('video-link');?>"><span>Смотреть</span></a>
                </div>
            </div>
        <?php
        endwhile;
    endif;
    wp_reset_postdata();
    die();
}


add_action('wp_ajax_loadmore', 'true_load_posts_videos');
add_action('wp_ajax_nopriv_loadmore', 'true_load_posts_videos');


i think the add_action lines are in conflict

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vladzvezdin, 2018-09-21
@vladzvezdin

Found a solution. Indeed, they clashed. Assigned different names, namely, instead of wp_ajax_loadmore, he registered his name wp_ajax_example. And also in the second line

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question