如何在 wordpress functions.php 中发送一组类别和帖子?

How to send an array of categories and posts in wordpress functions.php?

我向 functions.php 中的自定义端点函数发出请求:

add_action( 'rest_api_init', function () {
    register_rest_route( 'wp/v2', '/homepage/', array(
        'methods' => 'GET',
        'callback' => 'custom',
    ) );
} );

然后在 return 中,我得到了一个作者 ID 的帖子数组:

function custom( $data ) {
    $posts = get_posts( array(
        'author' => $data['17'],
    ) );
    
    if ( empty( $posts ) ) {
        return null;
    }

    return $posts;
}

我想 return 所有帖子和所有类别,但出现错误:

return [$posts , $categories ];

如何在自定义函数内的单个数组中获取所有帖子和所有类别?

好的,试试这个

function custom( $data ) {
     $posts = get_posts( array(
         'post_type' => 'post'
         'author' => $data['17'],
         'numberposts' => -1
     ) );
        
     $categories = get_categories();
    
     return [$posts, $categories];
 }

你做所有的事情都是这样的:

function custom( $data ) {
     $posts = get_posts( array(
         'post_type' => 'post'
         'author' => $data['17'],
         'numberposts' => -1
     ) );
        
     $categories = get_categories();
    
     return [$posts, $categories];
 }