首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏按钮“加载更多”时,没有更多的帖子可用。

隐藏按钮“加载更多”时,没有更多的帖子可用。
EN

Stack Overflow用户
提问于 2015-12-29 11:18:30
回答 2查看 2.5K关注 0票数 0

当没有更多的帖子可用时,我想隐藏load按钮。

这是我的密码

function.php

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-29 11:30:01

)如果没有post,只需检查php页面返回的内容。

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2015-12-29 11:29:37

您可以在phpecho -1上计数post,并在js上检查posts是否是-1禁用加载更多的按钮,如下所示

代码语言:javascript
复制
// 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34509882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档