0

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

苦苦挣扎:1.如何查询tumblr的多个标签?-- (这里有一个类似的帖子:如何在 Tumblr 的 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();
});
4

1 回答 1

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);
      });
 }
于 2012-07-26T12:42:06.667 回答