Пользователь пока ничего не рассказал о себе

Достижения

Все достижения (2)

Наибольший вклад в теги

Все теги (21)

Лучшие ответы пользователя

Все ответы (20)
  • Как добавить в запрос REST API в wp-json sticky posts?

    @drnkwtr Автор вопроса
    forblitz.ru dev
    Эй, потомки, если когда-нибудь вы зададитесь таким же вопросом, то вот вам решение (вписывать в functions.php)

    function prefix_get_endpoint_phrase() {
        // rest_ensure_response() wraps the data we want to return into a WP_REST_Response, and ensures it will be properly returned.
        $url1 = 'https://forblitz.ru/wp-json/wp/v2/posts?sticky=true'; // path to your JSON file
    	$url2 = 'https://forblitz.ru/wp-json/wp/v2/posts?per_page=30&categories_exclude=1'; // path to your JSON file
    	$json1 = file_get_contents($url1);
    	$json2 = file_get_contents($url2);
    
    	$my_array1 = json_decode($json1, true);
    	$my_array2 = json_decode($json2, true);
    	$res = array_merge($my_array1, $my_array2);
        return $res;
    }
     
    /**
     * This function is where we register our routes for our example endpoint.
     */
    function prefix_register_example_routes() {
        // register_rest_route() handles more arguments but we are going to stick to the basics for now.
        register_rest_route( 'wp/v1', '/posts', array(
            // By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended.
            'methods'  => WP_REST_Server::READABLE,
            // Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class.
            'callback' => 'prefix_get_endpoint_phrase',
        ) );
    }
     
    add_action( 'rest_api_init', 'prefix_register_example_routes' );
    Ответ написан
    Комментировать

Лучшие вопросы пользователя

Все вопросы (24)