隐藏按钮"加载更多"什么时候没有更多的帖子

时间:2015-12-29 11:18:31

标签: javascript php jquery wordpress

我想在没有更多帖子时隐藏load按钮。

这是我的代码

function.php

function more_news_ajax(){

    $offset = $_POST["offset"];
    $ppp = $_POST["ppp"];
    header("Content-Type: text/html");

    $args2 = array(
        'posts_per_page' => $ppp,
        'offset' => $offset,
        'post_type' => 'post',
    );

    $custom2 = new WP_Query($args2);

    while ($custom2->have_posts()) : $custom2->the_post(); 
    echo '<div class="blog-loop-article" id="evento-';echo get_the_ID(); echo' ">

                <div class="meta">
                    <p class="data">'; the_time('j F Y'); echo'</p>
                    <h3>'; the_title(); echo'</h3>
                    <a class="morepress" href="'; the_permalink(); echo'" rel="bookmark"> <p>read more</p></a>
                </div>';

                if(has_post_thumbnail()) :
                echo'
                <div class="abstract">'; echo custom_trim_excerpt(30); echo'</div>
                <div class="thumb">';
                    the_post_thumbnail('thumb-blog', array('class' => 'img-responsive')); 
                echo'</div>';
                else :
                echo'<div class="abstract large">'; echo custom_trim_excerpt(30); echo'</div>';
                endif;
            echo'</a>
        </div>';
endwhile;

exit;
}

add_action('wp_ajax_nopriv_more_news_ajax', 'more_news_ajax'); 
add_action('wp_ajax_more_news_ajax', 'more_news_ajax');

JS

<script type="text/javascript">
var ajaxUrl = "<?php echo admin_url('admin-ajax.php', null); ?>";
var page = 1; // What page we are on.
var ppp = 3; // Post per page

jQuery("#morenews").on("click",function(){ // When btn is pressed.
    jQuery("#morenews").attr("disabled",true); // Disable the button, temp.
    jQuery.post(ajaxUrl, {
        action:"more_news_ajax",
        offset: (page * ppp) + 3,
        ppp: ppp
    }).success(function(posts){
        page++;
        jQuery("#listnews").append(posts);
        jQuery("#morenews").attr("disabled",false);
    });

   });
</script>

2 个答案:

答案 0 :(得分:0)

您可以计算phpecho -1的帖子,并在js检查posts是否为-1禁用加载更多按钮

// counting post before while 
if(!$custom2->found_posts){
   echo "-1";
   exit;
}

while ($custom2->have_posts()) 
...


// in js ajax success checking if posts == "-1"
...
.success(function(posts){
    if(posts == "-1"){
        jQuery("#morenews").attr("disabled",true);
    }else{
       page++;
       jQuery("#listnews").append(posts);
       jQuery("#morenews").attr("disabled",false);
    }
});

答案 1 :(得分:0)

你几乎在;) 如果没有更多帖子,请检查您的php页面返回的内容。

<script type="text/javascript">
var ajaxUrl = "<?php echo admin_url('admin-ajax.php', null); ?>";
var page = 1; // What page we are on.
var ppp = 3; // Post per page

jQuery("#morenews").on("click",function(){ // When btn is pressed.
    jQuery("#morenews").attr("disabled",true); // Disable the button, temp.
    jQuery.post(ajaxUrl, {
        action:"more_news_ajax",
        offset: (page * ppp) + 3,
        ppp: ppp
    }).success(function(posts){
        page++;
        jQuery("#listnews").append(posts);
        if (posts != "")  // Mean if posts is different from an empty string
          jQuery("#morenews").attr("disabled",false); // If there are post we enable the button
    });

   });
</script>