简体   繁体   English

如何在 wordpress 函数中发送一系列类别和帖子。php?

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

I make a request to my custom endpoint function in functions.php:我在 functions.php 中向我的自定义端点 function 发出请求:

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

And in return I get an array of posts of an author id:作为回报,我得到一组作者 ID 的帖子:

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

    return $posts;
}

I want to return all posts and all categories but I get an error:我想返回所有帖子和所有类别,但出现错误:

return [$posts , $categories ];

How can I get All posts and all categories in a single array inside custom function?如何在自定义 function 中的单个数组中获取所有帖子和所有类别?

Ok try this好的试试这个

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

You do all of that like this:你这样做是这样的:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM