【问题标题】:Can't loop through posts inside a category loop. Only get the first item无法循环浏览类别循环内的帖子。只获取第一项
【发布时间】:2014-10-27 19:47:00
【问题描述】:

我正在尝试使用 php 为 wordpress 构建自定义 json 提要。

如您所见,它已经完成了。

问题是帖子的循环仅输出同一类别的一项。

这是我的 php 代码:

<?php
    header("Content-type: application/json");

    class Item {
        public $id = "";
        public $title  = "";
        public $thumb = "";
    }

    class Category {
        public $id = "";
        public $title = "";
        public $item = array();
    }    

    $finalData = array();
    //Get sub-categories from 'news'
    $idObj = get_category_by_slug('news'); 
    $id = $idObj->term_id;
    $cat_args=array(
        'orderby' => 'id',
        'order' => 'ASC',
        'parent' => $id
    );

    $categories=get_categories($cat_args);

    //Loop through categories
    foreach($categories as $category) {
        $args=array(
            'showposts' => 10,
            'category__in' => array($category->term_id),
            'caller_get_posts'=>1
        );
        $posts=get_posts($args);
        $a = new Category();
        $a->id = $category->term_id;
        $a->title  = $category->name;
        $arrayForItems = array();

        //Loop through first 10 posts from this categorie
        if ($posts) {
            $actualItem = $arrayForItems[] = new Item();
            $actualItem->id = get_the_ID();
            $actualItem->title = get_the_title();
            $img = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'appthumb' );
            $actualItem->thumb = $img;
        }
        $a->item = $arrayForItems;
        $finalData[] = $a;
    };

    echo json_encode($finalData);
?>

有什么想法吗?谢谢。

【问题讨论】:

    标签: php json wordpress loops posts


    【解决方案1】:

    那是因为你说只打印一次。添加一个while语句应该可以工作:

    $count = 0;
    while($count < 10)
    {
        if ($posts) {
                $actualItem = $arrayForItems[] = new Item();
                $actualItem->id = get_the_ID();
                $actualItem->title = get_the_title();
                $img = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'appthumb' );
                $actualItem->thumb = $img;
         }
         $count++;
    }
    

    编辑:这应该工作

    foreach ($posts as $post):
     setup_postdata($post);
            //Loop through first 10 posts from this categorie
            if ($posts) {
                $actualItem = $arrayForItems[] = new Item();
                $actualItem->id = get_the_ID();
                $actualItem->title = get_the_title();
                $img = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'appthumb' );
                $actualItem->thumb = $img;
            }
            $a->item = $arrayForItems;
            $finalData[] = $a;
     endforeach;
    

    【讨论】:

    • foreach 循环更适合这种情况,不是吗?
    • 谢谢豪林。现在我每个类别可以得到 10 个帖子,但帖子总是一样的!有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    相关资源
    最近更新 更多