Wordpress自定义查询分页和Ajax

时间:2013-08-30 20:37:46

标签: javascript ajax wordpress loops pagination

我的wordpress网站上有一些类别页面和单页,带有自定义查询。在主页上工作的分页基于一个脚本,该脚本在同一页面上加载帖子。

问题在于,分页仅适用于<?php previous_posts_link('&laquo; Previous') ?>的单页和类别 该脚本不适用于单个页面和类别。

以下是代码:

<div class="main_container">
<div id="load_posts_container">
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $args=array(
      'post_type'=>'post',
      'cat' => '-28' . $category_ID,
      'posts_per_page' => 8,
      'paged'=>$paged
   );
   $temp = $wp_query;
   $wp_query= null;
   $wp_query = new WP_Query($args);

   if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();   ?>
<!--ARTICLES MARKUP HERE-->
<?php   endwhile; endif; 
   ?>
<!--//load_posts_container-->
<
<div class="load_more_cont">
   <p>Altri eventi <br/>
      <span class="load_more_text"><?php next_posts_link('<img src="' . get_bloginfo('stylesheet_directory') . '/images/load-more-image.png" alt="Altri Eventi a Roma"/>', $wp_query->max_num_pages) ?></span>
   </p>
</div>
<!--//load_more_cont-->
<?php $wp_query = null;
   $wp_query = $temp;
   wp_reset_query(); ?>
<script type="text/javascript">
   // Ajax-fetching "Load more posts"
   $('.load_more_cont a').live('click', function(e) {
    e.preventDefault();
    //$(this).addClass('loading').text('Loading...');
           //$('.load_more_text a').html('Loading...');
    $.ajax({
        type: "GET",
        url: $(this).attr('href') + '#main_container',
        dataType: "html",
        success: function(out) {
            result = $(out).find('#load_posts_container .home_post_box');
            nextlink = $(out).find('.load_more_cont a').attr('href');
                           //alert(nextlink);
            //$('#boxes').append(result).masonry('appended', result);
                       $('#load_posts_container').append(result);
            //$('.fetch a').removeClass('loading').text('Load more posts');
                           //$('.load_more_text a').html('Load More');


            if (nextlink != undefined) {
                $('.load_more_cont a').attr('href', nextlink);
            } else {
                $('.load_more_cont').remove();
                                   $('#load_posts_container').append('<div class="clear"></div>');
                                 //  $('.load_more_cont').css('visibilty','hidden');
            }

                       if (nextlink != undefined) {
                           $.get(nextlink, function(data) {
                             //alert(nextlink);
                             if($(data + ":contains('home_post_box')") != '') {
                               //alert('not found');
                                 //                      $('.load_more_cont').remove();
                                                       $('#load_posts_container').append('<div class="clear"></div>');        
                             }
                           });                        
                       }

        }
    });
   });
</script>

有什么想法吗? 感谢

1 个答案:

答案 0 :(得分:0)

如果WordPress没有解析那些页面(我还没有验证过)的分页查询var,你可能想跳过get_query_var('paged'),只使用一个普通的旧时尚{ {1}}