【问题标题】:Calling Images in Text Posts Using Tumblr API使用 Tumblr API 调用文本帖子中的图像
【发布时间】:2011-04-02 14:50:41
【问题描述】:

我正在使用 Bandit Design (http://blog.bandit.co.nz/post/87511743/tumblr-featured-posts-javascript-plugin) 创建的精选帖子插件,我想将来自文本帖子的图像包含在精选帖子列表中(实际上只是一个帖子,图像如果您想知道,请在其下方添加帖子标题)。问题是 Tumblr API 不包含文本帖子中的照片参数(您可以调用整个帖子的正文,但这在这里并不理想)。我的 Javascript 知识的深度可以描述为“足以让事情以某种方式工作,但更有可能只是破坏事情”,所以这里的解决方案可能相当明显,我只是想念它。

我现在的代码:

/*
TUMBLR FEATURED POSTS SCRIPT
Automatically gets all posts tagged with "featured" and lists them
REQUIRES JQUERY!
--------------------------------------
Created by james <at> bandit.co.nz
http://blog.bandit.co.nz

Some code borrowed from Jacob DeHart's AJAX Search:
http://blog.bandit.co.nz/post/80415548/tumblr-ajax-inline-search
    */
    Featured = {
'apiNum' : 50, // 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
'linkAppend' : '', // html to append to the end of each linked post

'postDB' : [],
'listPos' : 0,
'doList' : function (where) {
    var li; var ul = $('#'+where);
    var titles = {"link":"link-text", "photo":"photo-caption", "quote":"quote-text", "regular":"regular-title", "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('<a class="'+p.type+'" href="'+p["url-with-slug"]+'">'+p["regular-body"]+titlestr+Featured.linkAppend+'</a>');
        ul.append(li);

        Featured.listPos = pcount;
    }
},

'getData' : function() {
    $.get('/api/read/json?num='+Featured.apiNum+'&tagged='+Featured.tagName,
        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();
    });

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery image tumblr


    【解决方案1】:

    您可以将创建li 的行更改为

    $(li).append($(p['regular-body']).find('img')[0])
         .append('<a class="'+p.type+'" href="'+p["url-with-slug"]+'">'+titlestr+Featured.linkAppend+'</a>');
    

    这将找到帖子正文的第一张图片,并将其添加到列表中的标题之前。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多