【问题标题】:How to export category name and category link in wordpress json如何在wordpress json中导出类别名称和类别链接
【发布时间】:2021-09-14 10:06:34
【问题描述】:

除类别名称和类别链接外,所有查询都有效,如果我使用“get_the_category()”,它会显示一些我不需要的东西,例如“category_count”、“category_description”等。

我需要知道如何按帖子类别分隔 jsons

这是我的代码


    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 10,
    );

    $query = new WP_Query( $args );
    $posts = array();
    

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

    $posts[] = array(
        'id' => get_the_ID(),
        'date' => get_the_date(),
        'title' => get_the_title(),
        'link' => get_the_permalink(),
        'image' => get_the_post_thumbnail_url(),
        'category' => get_the_category ( 'cat_name' ),
        'categoryLink' => get_category_link ( $category_id )

    );

    endwhile;

    wp_reset_query();

    $data = json_encode($posts);
    $upload_dir = wp_get_upload_dir();
    $file_name = date('Y-m-d') . '.json';
    $save_path = $upload_dir['basedir'] . '/' . $file_name;

    $f = fopen( $save_path , "w" ); 
    fwrite($f , $data);
    fclose($f);

}

add_action( 'save_post', 'export_posts_in_json' ); ```

【问题讨论】:

    标签: php json wordpress api


    【解决方案1】:

    get_the_category 需要一个帖子 ID(或布尔值 false) - 请参阅:https://developer.wordpress.org/reference/functions/get_the_category/

    get_category_link 需要一个整数或类别对象 - 请参阅:https://developer.wordpress.org/reference/functions/get_category_link/

    检查您的参数,看看是否有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2015-11-15
      • 2013-09-03
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多