link_header example

$collection_url = rest_url( rest_get_route_for_post_type_items( $this->post_type ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$collection_url );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            $response->link_header( 'prev', $prev_link );
        }
        if ( $max_pages > $page ) {
            $next_page = $page + 1;
            $next_link = add_query_arg( 'page', $next_page$base );

            $response->link_header( 'next', $next_link );
        }

        return $response;
    }

    
$base = add_query_arg( urlencode_deep( $request->get_query_params() )rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

        if ( $request['page'] > 1 ) {
            $prev_page = $request['page'] - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            $response->link_header( 'prev', $prev_link );
        }

        if ( $max_pages > $request['page'] ) {
            $next_page = $request['page'] + 1;
            $next_link = add_query_arg( 'page', $next_page$base );

            $response->link_header( 'next', $next_link );
        }

        return $response;
    }

    
$base_path      = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent']$this->rest_base ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$base_path );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            $response->link_header( 'prev', $prev_link );
        }
        if ( $max_pages > $page ) {
            $next_page = $page + 1;
            $next_link = add_query_arg( 'page', $next_page$base );

            $response->link_header( 'next', $next_link );
        }

        return $response;
    }

    
$collection_url = rest_url( rest_get_route_for_taxonomy_items( $this->taxonomy ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$collection_url );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            $response->link_header( 'prev', $prev_link );
        }
        if ( $max_pages > $page ) {
            $next_page = $page + 1;
            $next_link = add_query_arg( 'page', $next_page$base );

            $response->link_header( 'next', $next_link );
        }

        return $response;
    }

    


        $response = rest_ensure_response( $results );
        $response->header( 'X-WP-Total', $total );
        $response->header( 'X-WP-TotalPages', $max_pages );

        $request_params = $request->get_query_params();
        $base           = add_query_arg( urlencode_deep( $request_params )rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

        if ( $page > 1 ) {
            $prev_link = add_query_arg( 'page', $page - 1, $base );
            $response->link_header( 'prev', $prev_link );
        }
        if ( $page < $max_pages ) {
            $next_link = add_query_arg( 'page', $page + 1, $base );
            $response->link_header( 'next', $next_link );
        }

        return $response;
    }

    /** * Prepares a single search result for response. * * @since 5.0.0 * @since 5.6.0 The `$id` parameter can accept a string. * @since 5.9.0 Renamed `$id` to `$item` to match parent class for PHP 8 named parameter support. * * @param int|string $item ID of the item to prepare. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */
$base_path      = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent']$this->rest_base ) );
        $base           = add_query_arg( urlencode_deep( $request_params )$base_path );

        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            $response->link_header( 'prev', $prev_link );
        }
        if ( $max_pages > $page ) {
            $next_page = $page + 1;
            $next_link = add_query_arg( 'page', $next_page$base );

            $response->link_header( 'next', $next_link );
        }

        return $response;
    }

    
$response->header( 'X-WP-TotalPages', (int) $max_pages );

        $base = add_query_arg( urlencode_deep( $request->get_query_params() )rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
        if ( $page > 1 ) {
            $prev_page = $page - 1;

            if ( $prev_page > $max_pages ) {
                $prev_page = $max_pages;
            }

            $prev_link = add_query_arg( 'page', $prev_page$base );
            $response->link_header( 'prev', $prev_link );
        }
        if ( $max_pages > $page ) {
            $next_page = $page + 1;
            $next_link = add_query_arg( 'page', $next_page$base );

            $response->link_header( 'next', $next_link );
        }

        return $response;
    }

    
Home | Imprint | This part of the site doesn't use cookies.