使用 Tumblr API 调用文本帖子中的图像

发布于 2024-10-28 23:29:28 字数 2282 浏览 4 评论 0原文

我正在使用 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();
    });

任何帮助将不胜感激。

I'm using the Featured Posts Plugin created by Bandit Design (http://blog.bandit.co.nz/post/87511743/tumblr-featured-posts-javascript-plugin) and I'd like to include the images from text posts in the list of featured posts (which will actually just be a single post, the image with the post title below it, if you were wondering). The problem is that the Tumblr API doesn't include a parameter for photos within text posts (you can call the entire body of the post, but that's not ideal here). The depth of my Javascript knowledge could be described as "enough to somehow get things to work, but more likely just break things", so the solution is probably fairly obvious here and I'm just missing it.

The code I have right now:

/*
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();
    });

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下的泡沫是彩色的 2024-11-04 23:29:28

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

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

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

You can change the line where you create the li to

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

This will find the first image the post's body, and add it right before the title in the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文