2016-06-09 133 views
2

我很新的WordPress的。我目前使用的是FoundationPress主題,我正在嘗試添加一個按鈕,用戶可以點擊加載更多的帖子。WordPress的:添加無限滾動分頁

我想要的是,最初用戶會看到四篇博客文章,當用戶點擊閱讀更多按鈕時,它將加載下四篇文章,直到沒有更多文章和按鈕消失。

我已經到了可以加載前四個帖子的地步,但是我很難設置一個按鈕來顯示下面的四個帖子。

這是我到目前爲止有:

<section class="row blogArticleWrapper"> 
       <?php while (have_posts()) : the_post(); ?> 
       <?php 
       the_post(); 
       $blog_posts = get_posts(array(
        'post_type' => 'blog', 
        'posts_per_page' => 4, 
        'offset' => 1, 
        'orderby' => 'date', 
        'order' => 'DESC' 
       )); 
       if ($blog_posts): 
       ?> 
       <?php 
       foreach ($blog_posts as $post): 
        setup_postdata($post); 
        ?> 
        <article class="blog-profile small-12 columns"> 
         <a href="<?php the_field("news_url"); ?>" title="<?php the_title(); ?>"><span class="name"><?php the_field("news_title"); ?></span></a> 
         <p class="writtenBy">Written by: <?php the_field("news_written_by"); ?> <span class="date">Date: <?php the_field("news_date"); ?></span></p> 
        </article><!-- /.profile --> 
       <?php endforeach; 
       ?> 
       <?php endif; ?> 
       <?php endwhile; // end of the loop. ?> 
</section><!-- /.row --> 

我嘗試以下這個guide,但我不知道如何使用它我的網頁上。

任何幫助表示讚賞,

謝謝。

回答

1

刪除:

while (have_posts()) : the_post(); ?> 

       the_post(); 

<?php endwhile; // end of the loop. ?>

變更請求

$paged = get_query_var('paged') ? get_query_var('paged') : 1; 
$blog_posts = get_posts(array(
        'post_type' => 'blog', 
        'posts_per_page' => 4, 
        'offset' => 1, 
        'paged' => $paged, 
        'orderby' => 'date', 
        'order' => 'DESC' 
       )); 

添加到functions.php中

function wp_corenavi() { 
     global $wp_query; 
     $pages = ''; 
     $max = $wp_query->max_num_pages; 
     if (!$current = get_query_var('paged')) $current = 1; 
     $a['base'] = str_replace(999999999, '%#%', get_pagenum_link(999999999)); 
     $a['total'] = $max; 
     $a['current'] = $current; 

     $total = 1; 
     $a['mid_size'] = 3; 
     $a['end_size'] = 1; 
     $a['prev_text'] = '&laquo;'; 
     $a['next_text'] = '&raquo;'; 

     if ($max > 1) echo '<div id="pagination" class="navigation column medium-12">'; 
     if ($total == 1 && $max > 1) $pages = '<span class="pages">' . __('Page', 'Site') . $current . ' ' . __('of', 'Site') . ' ' . $max . '</span>'."\r\n"; 
     echo $pages . paginate_links($a); 
     if ($max > 1) echo '</div>'; 
    } 

endforeach;之後添加 - wp_corenavi();wp_reset_postdata();; 然後jQuery的AJAX:

//Trigger ajax at the end of page 
      $(window).scroll(function(){ 
        var top = $('body,html').scrollTop(); 
        var height = $(window).height(); 
        var docheight = $(document).height(); 

        var screen = Number(docheight) - Number(height); 

        if(top >= screen){ 
         $('#pagination .next').click(); 
        } 
       }); 

      //do ajax on pagination 
      $('#pagination .next').on('click',function(e){ 
       e.preventDefault(); 

       $('#pagination').remove(); 


       $.ajax({ 
        type: "GET", 
        url: $(this).attr('href'), 
        dataType: "html", 
        success: function(out){ 

         //We get blocks from next page , change selector to yours 
         result = $(out).find('.short-articles-wrapper-main .short-article'); 
         //find next page pagination,change selector to yours 
         pagination = $(out).find('.short-articles-wrapper-main #pagination');    
         //append blocks from next page to current ,change selector to yours 
         $('.short-articles-wrapper-main').append(result); 
         //append pagination from next page to current, change selector to yours 
         $('.short-articles-wrapper-main').append(pagination.fadeIn(200));  
        } 
       }); 
      }); 

希望它會幫助你。

+0

嗯,它似乎並沒有工作,我調試通過代碼的Ajax部分,它似乎變量'頂部'永遠不會改變,即使我在頁面的底部。 我將頂部變量改爲觸發if語句的東西,但即使調用'$('#pagination .next'),代碼的'ajax on pagination'部分似乎也不會被觸發。點擊();' – Radizzt