将AJAX分页添加到自定义帖子类型

时间:2018-06-06 07:06:25

标签: ajax wordpress pagination

在WordPress中,我使用以下代码按类别显示博客文章列表。有人可以请帮助我如何在每个帖子中添加AJAX分页,因为我只想在每页显示6个帖子。

   function get_cat_posts() {

       if ( !wp_verify_nonce( $_REQUEST['nonce'], 'mysite-cat-nav')) {
             exit('No naughty business please');
        } 

        $ids = array();
        if( isset($_POST['term_id']) && !empty($_POST['term_id']) ) {

            $term_id = $_POST['term_id'];
            $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
              $args = array(
                'post_type' => 'post',
                'order'     => 'DESC',
                'posts_per_page' => -1,
                'paged'     => $paged,
                'tax_query' => array(
                      array(
                        'taxonomy' => 'category',
                        'field'    => 'term_id',
                        'terms'    => $term_id,
                      ),
                    ),
              );

下面的代码是我的查询和循环按类别显示帖子

// The Query
          $the_query = new WP_Query( $args );

          // The Loop
          if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) {
              $the_query->the_post();
                $id = get_the_ID();
                $title = get_the_title();
                $categories = get_the_category();
                $content = wp_trim_words( get_the_content(), 12, '...' );
                $image = get_the_post_thumbnail();

                echo '<div class="col-container col-md-4">
                    <div id="post-'. $id . '" class="col rj-inner-border">
                        <div class="et_pb_image_container">' . $image . '</div>
                        <div class="rj-the-content">
                            <h2 class="rj-post-title">
                                <a href="'.get_the_permalink().'">'.$title.'</a>
                            </h2>
                            <p class="post-meta">';
                                $count = 1;
                                $category_count = count($categories);
                                foreach ($categories as $category) {
                                    $term_link = get_category_link( $category->term_id );

                                    if($count == $category_count){
                                        echo '<a href="' . $term_link .'" rel="category tag">'. $category->name .'</a> ';
                                    }else{
                                        echo '<a href="' . $term_link .'" rel="category tag">'. $category->name .'</a>, ';
                                    }
                                    $count++;
                                }  
                            echo '</p>
                            <div class="post-content" style="height: 89px;">
                                <p>'. $content.'</p>
                            </div>          

                        </div>
                    </div>
                </div>';
            }           

            wp_reset_postdata();
            } else {
        // no posts found
      }

一切都很完美我唯一的问题就是分页。

这是我通过点击类别标签显示帖子列表的ajax代码。

$(document).on('click', '#menu-menu-blog-categories li.menu-item', function(e) {
        e.preventDefault();
        var term_id = $(this).attr('data-term-id');


        $.ajax({
            url : formaliti.ajax_url,
            type : "POST",
            data : {
                action : 'get_cat_posts',
                nonce : formaliti.nonce,
                term_id : term_id
            },  
            beforeSend : function() {

            },
            success : function(response) {
                $('.fl-node-5b076b128e378').html(response);

            },
            error : function(err) {
                console.log(err);
            }
        });
    });

0 个答案:

没有答案