Using Tumblr Like Button with Infinite Scroll

ぐ巨炮叔叔 提交于 2019-11-26 14:25:27

问题


I'm trying to use the new Tumblr like buttons within Infinite Scroll (allowing your theme a like button on individual Tumblr posts from the homepage) they work for the first 15 posts of the first 'page' but then as soon as it loads another page the like button stops working. These are the instructions given from Tumblr on the Docs page:

Function: Tumblr.LikeButton.get_status_by_page(n)
Description: Call this function after requesting a new page of Posts. Takes the page number that was just loaded as an integer.

Function: Tumblr.LikeButton.get_status_by_post_ids([n,n,n])
Description: Request Like status for individual posts. Takes an array of post IDs.

As I'm not sure how to properly apply JQuery I'm not sure where to add these functions, here is my JS for my current theme:

    // MASONRY
    var $container = $('#content');

    $container.imagesLoaded( function(){
        $container.masonry({
            itemSelector: '.entry',
            columnWidth: 220
        });
    });

    // INFINITE SCROLL
    $container.infinitescroll({
        navSelector  : '#pagination',
        nextSelector : '#pagination li a.pagination_nextlink',
        itemSelector : '.entry',
        loading: {
            img: 'http://static.tumblr.com/glziqhp/K37m9yaub/257__1_.gif'
        }
    },

    function( newElements ) {
        var $newElems = $( newElements ).css({
            opacity: 0
        });
        $newElems.imagesLoaded(function(){
            $newElems.animate({
                opacity: 1
            });
            $container.masonry(
                'appended', $newElems, true
            ); 
        });
    });

回答1:


First you need to add a unique post ID to each of your posts:

<div class="entry masonry-brick" id="{PostID}">...</div>

The documentation mentions requesting the like status once the new posts (or new page) has been appended / loaded:

function( newElements ) {
    var $newElems = $( newElements ).css({
        opacity: 0
    });

    // Create Array of $newElems IDs
    var $newElemsIDs = $newElems.map(function () { 
        return this.id; 
    }).get();

    $newElems.imagesLoaded(function(){
        $newElems.animate({
            opacity: 1
        });
        $container.masonry(
            'appended', $newElems, true
        );

        // Let's just see what we have, remove console.log() if working
        console.log($newElems, $newElemsIDs);


        Tumblr.LikeButton.get_status_by_post_ids($newElemsIDs);
    });
});

I hope that points you in the right direction.



来源:https://stackoverflow.com/questions/16390193/using-tumblr-like-button-with-infinite-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!