tumblr基于多个标签的精选文章

时间:2012-07-26 12:02:27

标签: jquery tags tumblr

我正在尝试根据标签显示特定的tumblr帖子。

苦苦挣扎: 1.如何查询tumblr多个标签? - (这里的类似帖子:How to query multiple tags in Tumblr's read api?) - 但我如何将其改编为我的剧本? 2.根据标签将类分配给渲染的帖子。例如,如果帖子标记为“featured_1”,则应在呈现的html中显示“featured_1”类。

非常感谢提前!任何帮助表示赞赏。

这是我正在使用的脚本:

/*
--------------------------------------
    Created by james <at> bandit.co.nz
    http://blog.bandit.co.nz
*/
Featured = {
    'apiNum' : 100, // how many posts to read
    'listId' : 'featured', // the id of the ul to write to
    'tagName' : 'featured', // the name of the tag we're searching for
    'tagName2': 'featured_1',
    'tagName3': 'featured_2',
    'tagName4': 'featured_3',

    'postDB' : [],
    'listPos' : 0,
    'doList' : function (where) {
        var li; var ul = $('#'+where);
        var titles = {"link":"link-text", "photo":"photo-caption", "quote":"quote-text", "regular":"regular-body", "video":"video-caption"}



        // cycle through post database
        pcount = Featured.postDB.length;
        for(i=Featured.listPos;i<pcount;i++) {
            p = Featured.postDB[i];
            if(p[titles[p.type]] != '') titlestr = p[titles[p.type]].replace(/<\/?[^>]+>/gi, '');
            else titlestr = p['url'];

            li = document.createElement('li');
            $(li).html('<p class=" ">'+titlestr+'</p>');
            ul.append(li);

            Featured.listPos = pcount;
        }
    },

    'getData' : function() {
        $.get('/api/read/json?num='+Featured.apiNum+'&tagged='+Featured.tagName3+Featured.tagName+Featured.tagName2,
            function(data) {
                eval(data);
                for(i=0;i<tumblr_api_read.posts.length;i++) {
                    Featured.postDB.push(tumblr_api_read.posts[i]);
                    Featured.doList(Featured.listId);
                }

            }
        );
    }
};

$(document).ready(function(){
    Featured.getData();
});

1 个答案:

答案 0 :(得分:0)

一切都已存在,无论是在您的代码中,还是在您提供的SO帖子中。由于似乎没有办法获得多个标签(我不是Tumbl用户),你仍然需要查询它自己的每个标签。 (未经测试的代码,您可能会在某处修复丢失的{或拼写错误。)

修改您的getData功能。现在它只读取一个标签并返回一个jqXHR whislt将结果推送到你的数组。

 'getData' : function(tag) {
       return $.get('/api/read/json?num=' + Featured.apiNum + '&tagged=' + tag,
       function(data) {
          //do NOT use eval or a kitten will die somewhere
          Featured.postDB.push(data.posts); //change according to the result
       });
    }

添加新功能(从您提供的帖子中复制自Gary Green)。这将使用您想要的每个标记调用您之前的函数。当所有这些都完成后(注意when-then函数),你可以做任何你需要做的事情。

'myFunction' : function() {
    $.when(Featured.getData(Featured.tagName1), Featured.getData(Featured.tagName2) /*, and so on */)
      .then(function() {
         var posts = $.extend({}, arguments); //you might not need this
         Featured.doList(Featured.listId);
      });
 }
相关问题