具有多个类别的Wordpress自定义JSON提要

时间:2014-09-01 11:12:34

标签: json wordpress rss feed feeds

大家早上好。

可以拥有一个自定义JSON Feed,其中包含发布了10个或更多帖子的所有类别的最后10个帖子?

我正在通过dphiffer使用Wordpress的JSON API插件发布10个或更多帖子的所有类别循环

因此,如果我有14个类别,我将执行140个请求并使服务器过载。

我正在寻找一种方法来处理1或2个请求。就像第一次请求获得10个或更多帖子的类别索引一样,第二个请求包含所有数据。

由于

以下是我迄今为止开发的代码:

<script type="text/javascript">
    var slug = 'news'; // Main categorie
    var maxLength = 10; // Total of articles per categorie
    var wpurl = 'http://wpurl.com'; // WP URL
    var loadedValue, loadedTotal;
    var categories = new Array();
    var newsData = new Array();

    //Get main categorie index by slug
    $.ajax({
        url: wpurl+'/?json=get_category_index',
        type: 'GET',
        dataType: 'jsonp',
        success: function(data){
            for (i = 0; i < data.categories.length; i++) {
                var cat = data.categories[i];
                if(cat.slug == slug) {
                    get_all_categories(cat.id);
                }
            }
        },
        error: function(data){
          console.log(data);
        }
    });

    //Get all sub-categories from an categorie slug
    function get_all_categories(cat_index) {
        $.ajax({
            url: wpurl+'/?json=get_category_index&parent='+cat_index,
            type: 'GET',
            dataType: 'jsonp',
            success: function(data){
                for (i = 0; i < data.categories.length; i++) {
                    var cat = data.categories[i];
                    //Check if categorie have 10 (maxLength) or more posts. If so push it to array (categories)
                    if(cat.post_count >= maxLength) {
                        categories.push({
                            'index': cat.id,
                            'title': cat.title
                        });
                    }
                }
            },
            error: function(data){
              console.log(data);
            }
        }).done(function() {
            //Set loading
            var totalItem = categories.length*maxLength;
            var actualItem = 0;
            var loaded = 0;

            for(var i=1; i<=categories.length; i++){                    
                prepareCategory(categories[i-1].index, maxLength, i);

                function prepareCategory(categorieIndex, maxLenght, count)  {
                    var content;
                    $.ajax({
                        url: wpurl+'/api/get_category_posts?category_id='+categorieIndex+'&count='+maxLenght+'&status=publish',
                        type: 'GET',
                        dataType: 'jsonp',
                        success: function(data){
                            for (e = 0; e < data.posts.length; e++) {
                                //Loading percent
                                actualItem++;
                                loaded = (actualItem/totalItem)*100;
                                document.getElementById('loadingBox').innerHTML = 'Loading: '+Math.round(loaded)+'%';
                                //Post data
                                var post = data.posts[e];
                                var title = post.title;
                                var thumb = post.thumbnail_images.appthumb.url || 'content/images/noimage.png';
                                var newContent = '<div class="new"><a href="#"><img src="'+thumb+'" width="320" height="164"><div class="new_description">'+title+'</div></a></div>';
                                var newId = 'cat'+count+'new'+(e+1);
                                newsData.push({
                                    'id': newId,
                                    'content':newContent
                                });
                            }
                        },
                        error: function(data){
                          console.log(data);
                        }
                    });

                }
            }

        });
    };

    function loaded() {
        if (window.localStorage.length != 0)
        {
            localStorage.clear();
            setGlobalStorageAndRedirect();
        } else {
            setGlobalStorageAndRedirect();
        }

        function setGlobalStorageAndRedirect() {
            localStorage.setItem('postPerCat', maxLength);
            localStorage.setItem('catNumbs', categories.length);
            localStorage.setItem('wpurl', wpurl);
            localStorage.setItem('categoriesArray', JSON.stringify(categories));
            localStorage.setItem('newsData', JSON.stringify(newsData));
            window.location="main.html";
        }
    }
</script>

0 个答案:

没有答案