WordPress – 将类别、帖子和图像输出到 JSON

我正在尝试重新创建一个具有特定结构的 JSON 文件,但我对 PHP 不太熟悉。


我想列出所有使用过的/非空的类别,然后是每个类别中的所有帖子,然后是每个帖子中使用的所有图像/其他详细信息。


我不确定代码在循环中应该是什么样子。当我将它提供给 D3 脚本时,我需要 JSON完全像下面(括号):


{

    "project": "Farm", // website name

    "about": "What we have on the farm.",

    "categories": [ //post categories

        {

            "slug": "fruits",

            "title": "Fruits",

            "description": "All about fruits.",

            "posts": [ // all posts within a category

                {

                    "slug": "apples",

                    "title": "Apples",

                    "excerpt": "Apple trees and fruits.",

                    "tags": "tree, apple",

                    "post_images": [

                        { 

                            "id": 25, 

                            "title": "Apple trees.", 

                            "filename": "apple-trees.jpg", 

                            "desc": "Rows of apple trees.", 

                            "tags": "" 

                        },

                        (...)

到目前为止的 PHP 脚本(在functions.php):


function export_to_json() {

    

    global $post;

    

    $post_args = array(

        'post_type' => 'post',

        'post_status' => 'publish',

        'posts_per_page' => -1,

    );

    

    $cat_args = array(

        'orderby' => 'name',

        'order' => 'ASC'

    );

    

    $posts = array();

    

    $cats = get_categories( $cat_args );

    foreach( $cats as $cat ) {

        $query = new WP_Query( $post_args );

    

        while ( $query->have_posts() ): $query->the_post();

        


                    ]

                ]

            );  

    }

}


add_action('save_post', 'export_to_json');

该脚本是错误的,因为每个帖子都会重复类别,我希望正确嵌套类别中的所有帖子。


对此的任何帮助将不胜感激。谢谢。


温温酱
浏览 128回答 1
1回答

子衿沉夜

干得好:function export_to_json() {        global $post;        $categories = array();        $cat_args = array(        'orderby' => 'name',        'order' => 'ASC'    );    $cats = get_categories( $cat_args );        foreach( $cats as $cat ) {             $post_args = array(            'post_type' => 'post',            'post_status' => 'publish',            'posts_per_page' => -1,            'category__in' => $cat->term_id        );            $get_posts = array();                $posts = get_posts( $post_args  );                foreach($posts as $post){                        $tags = get_the_tags($post->ID);            $i = 0;            $tag_names = '';            $post_images = array();                        if( $tags) {                foreach( $tags as $tag ) { $i++;                    $comma = $i == count($tags) ? '' : ', ';                    $tag_names .= $tag->name .  $comma;                }            }                                   $images = get_post_gallery($post->ID, false);                        if($images){                $image_ids = explode(",", $images['ids']);                foreach ($image_ids as $image ){                    $img = wp_prepare_attachment_for_js($image);                    $post_images[] = array(                        "id" => $img['id'],                         "title" => $img['name'],                         "filename" => $img['filename'],                        "desc" => $img['description'], //Pulls the image description                        'tags' => $tag_names                    );                }                            }                                    $get_posts[] = array(                'slug' => $post->post_name,                'title' => get_the_title(),                'excerpt' => get_the_excerpt(),                'tags' => $tag_names,                'post_images' =>  $post_images            );        }                                    $categories[] = array(            'slug' => $cat->slug,            'title' => $cat->cat_name,            'description' => $cat->description,            'posts' => $get_posts        );            }        $output = array(        'project' => get_bloginfo('name'),        'about' => get_bloginfo('description'),        'categories' => $categories    );    $data = json_encode($output, JSON_PRETTY_PRINT);    }
打开App,查看更多内容
随时随地看视频慕课网APP