【问题标题】:Hide the button "load more" when no more posts are available当没有更多帖子可用时隐藏“加载更多”按钮
【发布时间】:2015-12-29 11:18:30
【问题描述】:

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

这是我的代码

函数.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>

【问题讨论】:

  • 你怎么知道没有帖子?你 PHP 返回什么?
  • 你为什么要多行点.........

标签: javascript php jquery wordpress


【解决方案1】:

你快到了;) 如果没有更多帖子,只需检查您的 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>

【讨论】:

    【解决方案2】:

    您可以在 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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 2016-07-16
      • 2017-12-21
      • 1970-01-01
      • 2019-05-21
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多