简体   繁体   English

无法遍历类别循环内的帖子。 仅获得第一项

[英]Can't loop through posts inside a category loop. Only get the first item

I'm trying to build a custom json feed for wordpress with php. 我正在尝试使用php为wordpress构建自定义json提要。

It is already done as you can see. 如您所见,它已经完成。

The problem is that the loop through posts only output one item from the same category. 问题在于,循环发布仅输出相同类别的一项。

Here is my php code: 这是我的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);
?>

Any idea? 任何想法? Thanks. 谢谢。

That is because you are saying to only print it once. 那是因为您说只打印一次。 Adding a while statement should work: 添加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++;
}

EDIT: This should work 编辑:这应该工作

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WordPress:在每个循环中遍历多个api获取。 仅返回第一个数组项的响应 - Wordpress: Iterate through multiple api fetches within for each loop. Only returns response for first array item MySQL在for(PHP)循环内执行INSERT。 INSERT仅在第一个循环上运行 - MySQL do an INSERT inside for(PHP) loop. INSERT only runs on first loop 在 WooCommerce 产品类别页面循环中获取帖子计数 - Get posts count inside WooCommerce product category page loop 在循环中获取$ request。 Laravel 5.8 - get $request inside loop. Laravel 5.8 无法在WordPress循环中按类别查询帖子 - Can not query posts by category in WordPress loop ajax调用仅显示每个循环中第一项的输入值。 需要它显示取决于按钮单击的上下文 - ajax call only showing input values for the first item in for each loop. Need it to display depending on context of button click 在PHP Wordpress函数的菜单项上按类别循环发布 - Loop posts by category on a menu item from functions PHP Wordpress 在category.php中的帖子循环内显示ACF字段 - Display ACF field inside posts loop in category.php Ajax只在while循环中获取数据的第一项 - Ajax get data only first item in while loop 仅在循环内显示子类别名称 - Show child category name ONLY inside the loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM