Blog: How to display a specific category by default

Want to display a specific category in your Blog widget by default? We’ve got a solution!

const SELECTED_CATEGORY_INDEX = 1;

const waitForElement = (selector, root = document) => new Promise(res => {
    const observer = new MutationObserver(() => {
        const element = root.querySelector(selector);
        if (element) {
            res(element);
            observer.disconnect();
        }
    });
    
    observer.observe(root, { childList: true, subtree: true });
});

(function selectCategory() {
  waitForElement('.es-snap-carousel-container').then(categoriesBar => {
    const categories = categoriesBar.querySelectorAll('a');
    categories[SELECTED_CATEGORY_INDEX - 1].click();
  });
})();

Set the number of the needed category in the 1st line (they are counted from left to right) and add the resulted code to the Custom JS field on the Settings tab of your widget’s settings.


Guys, was this solution helpful to you? Let us know in the comments :wink:

4 Likes