Wordress Loadmore ajax doens隐藏按钮

时间:2017-10-13 07:19:03

标签: javascript php jquery ajax wordpress

我在wordpress页面上遇到了麻烦,我目前正在工作..

我使用此代码>> 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');

2 个答案:

答案 0 :(得分:0)

你的js在哪里,你有没有得到ajax的反应呢?

答案 1 :(得分:0)

您可以使用此功能检测最后一个帖子并显示无或隐藏按钮。

这只是一个例子

<?php if (($loop->current_post +1) == ($loop->post_count)) {
  echo 'This is the last post'; ?>
   <style>
    #load-more-posts{display:none !important;}
   </style>
 <?php 
} ?>