简体   繁体   English

jQuery“加载更多帖子”如果最后发布则隐藏按钮

[英]jQuery “load more posts” hide button if last post

I am using jQuery to create a "load more posts" effect for the Wordpress loop. 我正在使用jQuery为Wordpress循环创建“加载更多帖子”效果。 Since the loop has all the posts already loaded and they just appear with jQuery I have not been able to hide the "load more" button when I reach the last one. 由于循环已经加载了所有帖子,并且它们仅与jQuery一起显示,当我到达最后一个帖子时,我无法隐藏“加载更多”按钮。

$(document).ready(function () {
$(".post").addClass("hide"); 
total = $("#allpost .post").size();
x = 3;

    $('.loadmore').click(function () {
       //y = x;
        $(total);
    $(".post").removeClass("hide");     
        $(".post:gt("+x+")").addClass("hide"); 
     x = x + 1;
    });
});

My loop 我的循环

<div id="all-posts">
        <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
        $args= array(
    'posts_per_page' => 100,
    'paged' => $paged
);
query_posts($args); ?> <!-- posts per page -->
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php $postcount++;
$new_class = ( ($postcount % 2) == 0 ) ? "even" : "odd"; ?>
<div <?php post_class($new_class) ?> id="post-<?php the_ID(); ?>">
    <div class="post_image">
        <a href="<?php echo the_permalink(); ?>">
        <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
            the_post_thumbnail('custom-size'); // post thumbnail    
        }?>
        <button class="blog_button">Vaata</button></a>
    </div>
    <div class="post_tekst">
        <div class="lisakast">
            <h2><?php the_title(); ?></h2>
            <p><?php the_excerpt(); ?></p>
        </div>
    </div>  
    </div><!-- odd / even div -->
        <?php endwhile; endif; ?>
              <div class="loadmore">Lae juurde</div>
        </div><!-- all posts -->

JSFiddle: https://jsfiddle.net/hd4603gj/7/ JSFiddle: https ://jsfiddle.net/hd4603gj/7/

How could this be achieved? 如何做到这一点?

You only need to add an extra validation at your JS code. 您只需要在JS代码上添加一个额外的验证即可。

$(document).ready(function () {
    $(".post").addClass("hide"); 
    total = $( ".post" ).length;
        x = 3;

    $('.loadmore').click(function () {
       //y = x;
        $(total);
    $(".post").removeClass("hide");     
        $(".post:gt("+x+")").addClass("hide"); 
     x++;


    if($(".post.hide").length == 0){
        $('.loadmore').addClass('hide');
    }

    });

When all posts are visible, then hide the "load more" button 当所有帖子都可见时,然后隐藏“加载更多”按钮

You can use the following code: 您可以使用以下代码:

$(document).ready(function () {
    $(".post").addClass("hide"); 
        total = $( ".post" ).length;
            x = 3;

        $('.loadmore').click(function () {
           //y = x;
            $(total);
        $(".post").removeClass("hide");     
            $(".post:gt("+x+")").addClass("hide"); 
         x++;


        if(x >= total){
        $( ".loadmore" ).hide();
        }

        });

    });

.length() function: .length()函数:

The number of elements currently matched. 当前匹配的元素数。 The .size() method will return the same value. .size()方法将返回相同的值。 To get more information about .length() : Click 获取有关.length()更多信息: 单击

Demo: Fiddle 演示: 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM