Since it is one of the most useful things You can use, the Popular Posts gadget provides You a great opportunity to be creative in composing a way it will be presented to Your Guests. It can take various forms, up to Your imagination - here are few examples I chose:
In theory it's good to have articles' thumbnails, titles and excerpts to tailor some good composition out of these - but not always You have the certainty that each post will be equipped with images (on the base of which thumbnails are created). So in case there is no image in the post - Your carefully designed composition would be disturbed. Fortunately there are several workarounds to give you the certainty mentioned above :) . Which one You'll choose depends on which technology You feel most comfortable working with. You also need to have an image which You will use as a default thumbnail (in case of articles with no images). The default thumbnail should be the same size (width & height) as current thumbnails used in Popular Posts gadget. Let's see:
I. CSS-based.
Short outline: find out which Popular Posts' element encloses single thumbnails and then ascribe a background to it.
II. jQuery-based.
Short outline: detect the Popular Post item without an image and then append a default thumbnail to it.
III. Blogger template only.
Short outline: put a default image to Popular Posts in case there is no image in the particular article.
As a curiosity: if You're all right with all of these approaches You would consider which one is the best (or is there any significant difference at all?) I'd say that editing the template itself would work the best because:
Let's have a closer look on how all those methods work:
I. CSS-based.
Suppose You already use the Popular Posts gadget (including posts' thumbnails) on Your site. It would be also very useful If You already know CSS - if not yet, then give this instruction a chance, fingers crossed :) !
#PopularPosts1 .item-thumbnail
{
Your CSS commands;
}/* (remember what is the class name in Your case - and if differs - replace "item-thumbnail" with it) */
- or, if there wasn't a line beginning with "<div" (see the steps 3 to 5 in the instruction above), but a sole "<span":
#PopularPosts1 span
{
Your CSS commands;
}
You'll probably use commands like:
/* here You define Your default thumbnail's location: */
background-image: url(http://PathToTheDefaultThumbnailImage.jpg);
/* here You can refine background's position; values are example; the first value is the horizontal position and the second value is the vertical - note that You can use negative numbers: */
background-position: -11px 36px;
/* there is no need to repeat the default thumbnail image within a single item, so: */
background-repeat: no-repeat;
A curiosity for Code-Lovers :)
It's probable that soon with CSS we will be able to use so-called "The Relational Pseudo-class :has()" - sounds familiar :) ? So far in order to benefit from this awesome thing I needed to use jQuery. To have it inside the very CSS... it would be wonderful :) .
II. jQuery-based.
I recommend this method only in case You don't feel comfortable enough to edit the actual template itself.
In this case using jQuery You have much more flexibility than with a CSS only. But You need to do a careful "back-engineering" - having a closer look on how exactly the Popular Posts gadget is composed in Your site's case - being more precise: You need to know what "box" (HTML element) is meant to contain a post's thumbnail - and what is placed just next to it (all those informations will serve You as reference points to compose proper jQuery code). To find this out we'll use Chrome DevTools:
Now You have everything You need to construct properly tailored jQuery code. First let's summarize what emerges out of our examination:
Now, imagine that one particular post displayed in Popular Posts gadget does not have a thumbnail. In this case the first element inside a parent box "item-content" will be the "item-title". If a post has a thumbnail, the first element here should be the "item-thumbnail".
What You need to do is simply to fill the gap - by inserting missing "item-thumbnail" elements (divs) right in place where they usually are used to be. Thinking in terms of jQuery You need to write code which detects a parent element without an image (thumbnail) - and puts a default image right in place. Following our example the code could be like this:
$('#PopularPosts1 .item-content').not(":has(img)").each(function() {
$( ".item-title", this ).before( "<div class='item-thumbnail'><img src='http://thePathtoYourDefaultThumbnailImage.jpg' /></div>" );
});
In other words: if item-content has no images - then put a traditional "image box" (item-thumbnail' containing Your default thumbnail image) right before item-title.
Now last thing You need is to place that code into a HTML/JavaScript gadget in which You're collecting all jQuery instructions. See how to create it here.
III. Blogger template only.
Last but not least - the easiest way of all (provided You are fine with Blogger template editing). This method consists on "teaching the Popular Posts gadget" what to do if there is no thumbnail attached to the article.
This gadget can be composed in a variety of ways (it depends on the template You chose for Your site) - so unfortunately there is no guarantee for an universal method, but if You are all right with template editing, then I think You will managed this well :) .
<img alt='' border='0' expr:height='data:thumbnailSize' expr:src='data:post.thumbnail' expr:width='data:thumbnailSize'/>
<b:else/>
<img src='http://thePathToYourDefaultThumbnailImage.png' />
No comments:
Post a Comment