为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
温温酱 2023-06-18 16:16:37
我正在尝试重新创建一个具有特定结构的 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');该脚本是错误的,因为每个帖子都会重复类别,我希望正确嵌套类别中的所有帖子。对此的任何帮助将不胜感激。谢谢。
查看完整描述

1 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

干得好:


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);

    

}



查看完整回答
反对 回复 2023-06-18
  • 1 回答
  • 0 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信