Pull the first image from a Tumblr photoset?

ぐ巨炮叔叔 提交于 2019-12-03 05:04:31

Tumblr markup:

{block:Photoset}
    <div class="photoset-wrap">
    {block:Photos}
        <img src="{PhotoURL-500}" />
    {/block:Photos}
    </div>
{/block:Photoset}

Note: may be you need {PhotoURL-75sq} to output square preview instead of 500px image;

CSS:

.photoset-wrap img { display: none; }
.photoset-wrap img:first-child { display: block; }

the above example is flawed as the user has to download all the images even though only the first is shown so instead I've used jquery to inject only the first image.

HTML (simplified):

{block:Photoset}
    <div class="photoset">
        {block:Photos}
            <div class="photoPlaceholder" imgsrc="{PhotoURL-500}"></div>
        {/block:Photos}
    </div>
{block:Photoset}

javascript (simplified):

$(document).ready(function() {
    $(".photoset .photoPlaceholder").first().each(function (i) {
        var src = $(this).attr("imgsrc");
        $(this).html('<a href="#"><img src="'+ src +'" /></a>');
    });
});

This ensures only the first image is loaded... you can always have the other css example above in a noscript tag incase they have javascript turned off.

Hope that helps

There doesn't seem to be a way to limit them, but you can comment them out. This solution has the advantage that the hidden images won't be loaded at all and it doesn't need any javascript

{block:Photoset}
    <!-- Go through each Photo in the Photoset -->
    {block:Photos}
        <img src="{PhotoURL-HighRes}" class="highres">
        <!--
    {/block:Photos}
    -->

    {block:Caption}
        <p>{Caption}</p>
    {/block:Caption}
{/block:Photoset}

See that <!-- before {/block:Photos}? That opens an HTML comment right after the first image, so the rest of the images in the loop will be in a comment, invisible. After the loop we close the comment with -->. Done, all the images in the photoset after the first one will be hidden.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!