0

i am working with wp now but it is quite limited (can't access to php, plugins,..). I want to find JAVASCRIPT code to retrieve rss feed from url and arrange the feed title by ALPHABETICAL order.

JS code can be added to footer and retrieve rss feeds in another separate page. TYVM :)

Linh K
  • 1

1 Answers1

0

Of course, its just show to you how to get feed by jQuery, then you can make improvement about those code to make it ALPHABETICAL, sort() is your friend. Here my answer, I use SO WordPress tag feed for example, and jQuery Ajax methode base on @haylem answer How to parse an RSS feed using JavaScript?

<script>
jQuery(document).ready(function($) {
        FEED_URL = 'https://stackoverflow.com/feeds/tag?tagnames=wordpress&sort=newest';//feed url
        $.ajax({
            url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent(FEED_URL),
            dataType: 'json',
            success: function(data) {
                if (data.responseData.feed && data.responseData.feed.entries) {
                    var ul = $('<ul>'),
                        titles = data.responseData.feed.entries.map(function(item) {
                            return item.title;
                        }); //get the titles in array
                    $.each(titles.sort( case_insensitive_comp ) /* sort the titles */,
                    function(i, r) {
                        var li = $('<li/>', {
                                'text': r
                            })
                            .appendTo(ul);
                    });
                    $('#element').html(ul); //output at #element
                }
            }
        })
    });

    //case insensitive base on @Lekensteyn answer at https://stackoverflow.com/a/5286047/1562904
    function case_insensitive_comp( strA, strB ) {
        return strA.toLowerCase().localeCompare( strB.toLowerCase() );
    }
</script>
Community
  • 1
  • 1
Jevuska
  • 480
  • 5
  • 15