AddThis Academy

An ever-growing library of resources to help you become a better online marketer.

Filter Related Posts URLs Using linkFilter

Note: This documentation applies to our Smart Layers tools, which have been deprecated.

Built into Smart Layers is the ability to filter what links show up in the What’s Next and Related Layers. This is done through the linkFilter property, which is documented in our Smart Layers API. This property will filter out links from appearing within your related items, based on strings of text that are specified. The ability to pick the link you want to have show up or add your own links isn’t possible with linkFilter, as it’s only intended to provide the ability to filter out what you don’t want to display.

Here are a few examples of how linkFilter can be used to filter out content, based on a fictional blog post about kittens doing yoga (who wouldn’t love that?!). In the below example, linkFilter is being used to remove any content that has the word “kitten” within the title.

addthis.layers({
    'share' : {},
    'whatsnext' : {},
    'recommended' : {},
    'linkFilter' : function(link, layer) {
        if (link.title && link.title.indexOf('kitten') >= 0) {
            return false;
        } else {
            return link;
        }
    }
});

Here’s a slightly more complicated example where specific titles and URLs can be filtered:

addthis.layers({
    'share' : {},
    'whatsnext' : {},
    'recommended' : {},
    'linkFilter' : function(link, layer) {
        if (link.url && link.url.indexOf('http://www.mysite.com/kittens-doing-yoga') === 0) {
            return false;
        } else if (link.title && link.title === 'Kittens doing yoga') {
            return false;
        } else {
            return link;
        }
    }
});

The previous example works great for filtering out a single string or URL. But suppose you had multiple pages of kittens doing yoga. Here’s another example that shows how to filter multiple URLs:

addthis.layers({
    'share' : {},
    'whatsnext' : {},
    'recommended' : {},
    'linkFilter' : function(link, layer) {
        if (link.url && (link.url.indexOf('http://www.mysite.com/kittens-doing-yoga') === 0 
           || link.url.indexOf('http://www.mysite.com/more-kittens-doing-yoga') === 0)) {
            return false;
        } else if (link.title && link.title === 'Kittens doing yoga') {
            return false;
        } else {
            return link;
        }
    }
});
Filter Related Posts URLs Using linkFilter
1 (20%) 1 vote

Was this article helpful to you?