2017-12-17

How to get Label Count outside widgets in Blogger

Blogger’s data tag “label.count” is only available within Labels widgets.

To get the label count (post number per label) outside the widget, you need to use JavaScript.

This is the simple JavaScript snippet: store the pair of the “Label Name / Count” from the Labels widget into an object (aka associative array).

JavaScript
var labelCounts = {};
var label = document.getElementById('Label1');
// 'div ul li' is for List, 'div>span' is for Cloud 
var elms = label.querySelectorAll('div ul li, div>span');
[].forEach.call(elms, function(elm) {
  if (elm.childElementCount == 2) {
    labelCounts[elm.children[0].innerHTML] = Number(elm.children[1].innerHTML.replace(/[()]/g, ''));
  }
});

Then, you can get the count of labelName by:

JavaScript
labelCounts["labelName"]

Prerequisite

  1. Labels widget ID is “Label1”.
  2. Check “Show number of posts per label” in “Configure Labels”.

3 comments:

  1. How can I add it to blogger?

    ReplyDelete
  2. You can add in ″Edit HTML″. I use this function for sorting breadcrumbs. See
    https://nkurilog.blogspot.com/2017/12/blogger-sort-breadcrumbs-by-label.html

    ReplyDelete
  3. Hi!

    This shows all the labels but how to get the all posts of a specific label?

    label (100)

    ReplyDelete