2017-10-13 59 views
0

我有我目前的工作WordPress的網頁上的麻煩..Wordress Loadmore AJAX好好嘗試一下隱藏按鈕

我用這個代碼>>https://github.com/alexcavender/wordpress-ajax-load-more 一些調整(以下完整代碼),但「負載更多按鈕不隱藏時,它到達的最後一個職位。

<div id="primary" class="content-area container"> 
    <main id="main" class="site-main" role="main"> 

    <?php if (have_posts()) : ?> 
     <div class="entry-content"> 
      <?php the_content(); ?> 
      <?php 
      wp_link_pages(array(
       'before' => '<div class="page-links">' . esc_html__('Pages:', 'thinkbit'), 
       'after' => '</div>', 
      )); 
      ?> 
     </div><!-- .entry-content --> 

     <div id="all-post" class="all-post wrapper"> 
      <?php 

      $args = array(
       'post_type' => array('post', 'project', 'communities'), 
       'posts_per_page' => 10 
      ); 
      $the_query = new WP_Query($args); ?> 
      <?php if ($the_query->have_posts()) : ?> 
       <div class="row masonry"> 
        <?php while ($the_query->have_posts()) : $the_query->the_post(); 
         /*$termsArray = get_the_terms($post->ID, "category, project-category, community-category"); //Get the terms for this particular item*/ 
         $termsArray = get_the_terms($post->ID, "category, project-category, community-category"); //Get the terms for this particular item 
         $termsString = ""; //initialize the string that will contain the terms 
         foreach ($termsArray as $term) { // for each term 
          $termsString .= $term->slug . ' '; //create a string that has all the slugs 
         } 
         ?> 

         <?php if (get_post_type() == 'project') : ?> 
          <div class="item"> 
           <div class="post-box"> 
            <?php if (has_post_thumbnail()) { 
             the_post_thumbnail('full', array('class' => 'img-responsive block image')); 
            } ?> 

            <a href="<?php the_permalink(); ?>" class="post-overlay"> 
             <h1><?php $post_type = get_post_type($post->ID); 
              echo $post_type; ?></h1> 
             <h3 class="text"><?php the_title(); ?></h3> 
             <h2><?php echo limit_words(get_the_excerpt(), '10'); ?> [...]</h2> 
            </a> 
           </div> 
          </div> <!-- end item --> 
         <?php endif; ?> 

         <?php if (get_post_type() == 'communities') : ?> 
          <div class="item"> 
           <div class="post-box"> 
            <?php if (has_post_thumbnail()) { 
             the_post_thumbnail('full', array('class' => 'img-responsive block image')); 
            } ?> 

            <a href="<?php the_permalink(); ?>" class="post-overlay"> 
             <h3 class="text"><?php the_title(); ?></h3> 
             <h2><?php echo limit_words(get_the_excerpt(), '10'); ?> [...]</h2> 
            </a> 
           </div> 
          </div> <!-- end item --> 
         <?php endif; ?> 

         <?php if (get_post_type() == 'post') : ?> 
          <div class="item"> 
           <div class="home-news-box"> 
            <a href="<?php the_permalink(); ?>"> 
             <h1 class="news">News</h1> 
             <h3 class="news-title"><?php the_title(); ?></h3> 
            </a> 
           </div> 

           <a href="<?php the_permalink(); ?>" class="post-overlay"> 
            <h1><?php $post_type = get_post_type($post->ID); 
             echo $post_type; ?></h1> 
            <h2><?php echo limit_words(get_the_excerpt(), '10'); ?> [...]</h2> 
           </a> 
          </div> <!-- end item --> 
         <?php endif; ?> 

         <div id="things-go-here"></div> 
        <?php endwhile; ?> 

       </div> <!-- end isotope-list --> 

       <?php wp_reset_postdata(); ?> 
      <?php endif; ?> 
     </div> 

     <div id="load-more-posts" class="text-center"> 
      <h4>Load More</h4> 
     </div> 

    <?php else : ?> 
     <?php get_template_part('template-parts/content', 'none'); ?> 
    <?php endif; ?> 

    </main><!-- #main --> 
</div><!-- #primary --> 

function load_more_posts() { 
    $nonce_check = check_ajax_referer('extra-special', 'security'); 

    if ($nonce_check) { 

    $offset = $_POST['offset']; 

    $args = array(
     'post_type' => array('post', 'project', 'communities'), 
     'post_status' => 'publish', 
     'posts_per_page' => '10', 
     'orderby' => 'date', 
     'order' => 'DESC', 
     'offset' => $offset, 
    ); 

    $loop = new WP_Query($args); 

    if ($loop->have_posts()) { 


     while ($loop->have_posts()) { 
      $loop->the_post(); 
      ?> 

      <article> 


       <?php if (get_post_type() == 'project') : ?> 
        <div class="item"> 
         <div class="post-box"> 
          <?php if (has_post_thumbnail()) { 
           the_post_thumbnail('full', array('class' => 'img-responsive block image')); 
          } ?> 

          <a href="<?php the_permalink(); ?>" class="post-overlay"> 
           <h1><?php $post_type = get_post_type($post->ID); 
            echo $post_type; ?></h1> 
           <h3 class="text"><?php the_title(); ?></h3> 
           <h2><?php echo limit_words(get_the_excerpt(), '10'); ?> [...]</h2> 
          </a> 
         </div> 
        </div> <!-- end item --> 
       <?php endif; ?> 


       <?php if (get_post_type() == 'communities') : ?> 
        <div class="item"> 
         <div class="post-box"> 
          <?php if (has_post_thumbnail()) { 
           the_post_thumbnail('full', array('class' => 'img-responsive block image')); 
          } ?> 

          <a href="<?php the_permalink(); ?>" class="post-overlay"> 
           <h3 class="text"><?php the_title(); ?></h3> 
           <h2><?php echo limit_words(get_the_excerpt(), '10'); ?> [...]</h2> 
          </a> 
         </div> 
        </div> <!-- end item --> 
       <?php endif; ?> 


       <?php if (get_post_type() == 'post') : ?> 
        <div class="item"> 
         <div class="home-news-box"> 
          <a href="<?php the_permalink(); ?>"> 
           <h1 class="news">News</h1> 
           <h3 class="news-title"><?php the_title(); ?></h3> 
          </a> 
         </div> 

         <a href="<?php the_permalink(); ?>" class="post-overlay"> 
          <h1><?php $post_type = get_post_type($post->ID); 
           echo $post_type; ?></h1> 
          <h2><?php echo limit_words(get_the_excerpt(), '10'); ?> [...]</h2> 
         </a> 
        </div> <!-- end item --> 
       <?php endif; ?> 



      </article> 
      <?php 

     } 
     wp_reset_postdata(); 
    } 
    wp_die(); 
    } else { 
    exit(); 
    } 
} 

add_action('wp_ajax_load_more_posts', 'load_more_posts'); 
add_action('wp_ajax_nopriv_load_more_posts', 'load_more_posts'); 
+0

我沒有看到任何AJAX或加載更多的按鈕 – madalinivascu

+0

哪裏是你的js? – madalinivascu

回答

0

哪裏是你的JS,你抓住Ajax響應了嗎?