wp query - Ajax load more inside custom post type taxonomy

I have created a new CPT called Event and inside event the taxonomy ( or term I dont know the correct name) called month

I have created a new CPT called Event and inside event the taxonomy ( or term I dont know the correct name) called month with 12 months inside.

So far I managed to display all month and filter it by ajax, but inside it month it is display only 3 posts per time and when I click in load more ajax button it ignores the taxonomy-month.php and displays all the events. It works fine in the view all, but not inside each month.

Please take a look in the january tab as example: /

my taxonomy-month.php

<div id="post-ajax" class="eventb">  
                   <div id="inside">  
                          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                              <section class="blocks">
                                   <div class="content_element">
                                       <a href="<?php echo get_permalink();?>"><h2><?php the_title(); ?></h2></a>
                                       <div class="content_text">
                                            <p><?php the_content(); ?></p>
                                            <p>Month: <?php the_terms( $post->ID, 'month', ' ', ' / ' ); ?></p>
                                        </div>
                                    </div>
                                    <div class="single_image_element">
                                         <div class="imgwrap"><div class="memberimg"><?php the_post_thumbnail(); ?></div></div>
                                   </div>
                              </section>
                            <?php endwhile; endif; ?>           
                   </div>
                   <div class="clear"></div>
       <div class="post_nav">
          <div class="loadmore">Load More</div>
       </div>

Script

var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
            var page = 2;
           jQuery(function($) {
               $('body').on('click', '.loadmore', function() {
               var data = {
    'action': 'load_posts_by_ajax',
    'page': page,
    'security': '<?php echo wp_create_nonce("load_more_posts"); ?>'
};

$.post(ajaxurl, data, function(response) {
    $('#inside').append(response);
    page++;
});
               });
            });

FUNCTION

function load_posts_by_ajax_callback() {
   check_ajax_referer('load_more_posts', 'security');
   $paged = $_POST['page'];
   $args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => '3',
'paged' => $paged,
     );
$my_posts = new WP_Query( $args );
if ( $my_posts->have_posts() ) :
    ?>
<article>
<?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>
          <section class="blocks">

                       <div class="content_element">

                           <a href="<?php echo get_permalink();?>"><h2><?php the_title(); ?></h2></a>
                             <div class="content_text">
                       <p><?php the_content(); ?></p>
                               <p>Month: <?php the_terms( $post->ID, 'month', ' ', ' / ' ); ?></p>
                                </div>
                        </div>

                            <div class="single_image_element">
                            <div class="imgwrap"><div class="memberimg"><?php the_post_thumbnail(); ?></div></div>
                           </div>

                    </section>
<?php endwhile ?>
</article>

I have created a new CPT called Event and inside event the taxonomy ( or term I dont know the correct name) called month with 12 months inside.

So far I managed to display all month and filter it by ajax, but inside it month it is display only 3 posts per time and when I click in load more ajax button it ignores the taxonomy-month.php and displays all the events. It works fine in the view all, but not inside each month.

Please take a look in the january tab as example: http://esma1.staging.wpengine/trade-fairs/

my taxonomy-month.php

<div id="post-ajax" class="eventb">  
                   <div id="inside">  
                          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                              <section class="blocks">
                                   <div class="content_element">
                                       <a href="<?php echo get_permalink();?>"><h2><?php the_title(); ?></h2></a>
                                       <div class="content_text">
                                            <p><?php the_content(); ?></p>
                                            <p>Month: <?php the_terms( $post->ID, 'month', ' ', ' / ' ); ?></p>
                                        </div>
                                    </div>
                                    <div class="single_image_element">
                                         <div class="imgwrap"><div class="memberimg"><?php the_post_thumbnail(); ?></div></div>
                                   </div>
                              </section>
                            <?php endwhile; endif; ?>           
                   </div>
                   <div class="clear"></div>
       <div class="post_nav">
          <div class="loadmore">Load More</div>
       </div>

Script

var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
            var page = 2;
           jQuery(function($) {
               $('body').on('click', '.loadmore', function() {
               var data = {
    'action': 'load_posts_by_ajax',
    'page': page,
    'security': '<?php echo wp_create_nonce("load_more_posts"); ?>'
};

$.post(ajaxurl, data, function(response) {
    $('#inside').append(response);
    page++;
});
               });
            });

FUNCTION

function load_posts_by_ajax_callback() {
   check_ajax_referer('load_more_posts', 'security');
   $paged = $_POST['page'];
   $args = array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => '3',
'paged' => $paged,
     );
$my_posts = new WP_Query( $args );
if ( $my_posts->have_posts() ) :
    ?>
<article>
<?php while ( $my_posts->have_posts() ) : $my_posts->the_post() ?>
          <section class="blocks">

                       <div class="content_element">

                           <a href="<?php echo get_permalink();?>"><h2><?php the_title(); ?></h2></a>
                             <div class="content_text">
                       <p><?php the_content(); ?></p>
                               <p>Month: <?php the_terms( $post->ID, 'month', ' ', ' / ' ); ?></p>
                                </div>
                        </div>

                            <div class="single_image_element">
                            <div class="imgwrap"><div class="memberimg"><?php the_post_thumbnail(); ?></div></div>
                           </div>

                    </section>
<?php endwhile ?>
</article>
Share Improve this question asked May 30, 2018 at 18:04 Leandro AndradeLeandro Andrade 171 gold badge1 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Assuming the function hooked to the action load_posts_by_ajax is your load_posts_by_ajax_callback() code (seems a reasonable assumption), the query arguments can be easily modified.

You mention "displays only 3 posts" which is what the code is designed to do. If you wish to show 10 posts, update the posts_per_page argument listed.

'posts_per_page' => '3', 

The bigger portion of your question is addressed by a taxonomy query. This is how you limit your query results to a specific term (january) within a specific taxonomy (month). I am guessing on the term slug and taxonomy name in this answer, however.

Assuming there is a month taxonomy registered for the event post type, something like this for your query should work:

function load_posts_by_ajax_callback() {
  check_ajax_referer('load_more_posts', 'security');
  $paged = $_POST['page'];
  $args = array(
    'post_type' => 'event',
    'post_status' => 'publish',
    'posts_per_page' => '10', // assuming you want 10 posts returned
    'tax_query' => array( // a tax query is always an array of arrays
      array(
        'taxonomy' => 'month', // assuming again; your taxonomy slug
        'field' => 'slug',
        'terms' => 'january', // you will want to assign this programmatically from the JS; not evident in the code provided
      )
    ),
    'paged' => $paged,
  );

 $my_posts = new WP_Query( $args );
 // remainder of function code is unchanged...

Several assumptions made in this answer. If you can add the relevant code to your question I might be able to update.

EDIT: I'm not 100% confident in the paging aspect. The code you provided makes use of paging and IIRC, this should be done with the offset argument for WP_Query. Separate question perhaps? Let's solve the Ajax tax query first.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745581476a4634280.html

相关推荐

  • wp query - Ajax load more inside custom post type taxonomy

    I have created a new CPT called Event and inside event the taxonomy ( or term I dont know the correct name) called month

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信