通过javascript

时间:2016-03-10 07:07:48

标签: javascript wordpress rss alphabetical

我现在正在使用wp,但它非常有限(无法访问php,插件,...)。我想找到JAVASCRIPT代码从URL检索rss feed并按ALPHABETICAL顺序排列feed标题。

JS代码可以添加到页脚并在另一个单独的页面中检索rss feed。 TYVM:)

1 个答案:

答案 0 :(得分:0)

当然,它只是向您展示如何通过jQuery获取提要,然后您可以对这些代码进行改进以使其成为ALPHABETICAL,sort()是您的朋友。在这里我的回答是,我使用SO WordPress标签Feed,以及基于@haylem的jQuery Ajax方法回答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>