【问题标题】:Featured post in tumblr based on multiple tagstumblr 中基于多个标签的精选帖子
【发布时间】:2012-07-26 12:02:26
【问题描述】:

我正在尝试根据标签显示特定的 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();
});

【问题讨论】:

    标签: jquery tags tumblr


    【解决方案1】:

    一切都已经存在,无论是在您的代码中,还是在您提供的 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);
          });
     }
    

    【讨论】:

    • 您好!非常感谢您的及时回复。最后我还需要打电话吗? (仍然无法正常工作,尽管一切都说得通) $(document).ready(function(){ Featured.getData(); });
    • 另外,知道如何将段落类设置为查询 api 后找到的标签名称吗?非常感谢! $(li).html('

      '+titlestr+'

      ');
    • 只是想弄清楚如何通过 tagName 设置 css 类。例如如果 API 查询找到标记为“tagNameX”的帖子,则应为其分配一个 CSS 类“tagNameX”。再次,Colossos 博士,非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    相关资源
    最近更新 更多