簡體   English   中英

如何在 wordpress 函數中發送一系列類別和帖子。php?

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

我在 functions.php 中向我的自定義端點 function 發出請求:

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

作為回報,我得到一組作者 ID 的帖子:

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

    return $posts;
}

我想返回所有帖子和所有類別,但出現錯誤:

return [$posts , $categories ];

如何在自定義 function 中的單個數組中獲取所有帖子和所有類別?

好的試試這個

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];
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM