On this page
WP_REST_Menu_Items_Controller::prepare_links( WP_Post $post ): array
Prepares links for the request.
Parameters
$postWP_Post Required-
Post object.
Return
array Links for the given post.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php. View all references
protected function prepare_links( $post ) {
$links = parent::prepare_links( $post );
$menu_item = $this->get_nav_menu_item( $post->ID );
if ( empty( $menu_item->object_id ) ) {
return $links;
}
$path = '';
$type = '';
$key = $menu_item->type;
if ( 'post_type' === $menu_item->type ) {
$path = rest_get_route_for_post( $menu_item->object_id );
$type = get_post_type( $menu_item->object_id );
} elseif ( 'taxonomy' === $menu_item->type ) {
$path = rest_get_route_for_term( $menu_item->object_id );
$type = get_term_field( 'taxonomy', $menu_item->object_id );
}
if ( $path && $type ) {
$links['https://api.w.org/menu-item-object'][] = array(
'href' => rest_url( $path ),
$key => $type,
'embeddable' => true,
);
}
return $links;
}
Related
Uses
| Uses | Description |
|---|---|
| WP_REST_Menu_Items_Controller::get_nav_menu_item() wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php | Gets the nav menu item, if the ID is valid. |
| rest_get_route_for_post() wp-includes/rest-api.php | Gets the REST API route for a post. |
| rest_get_route_for_term() wp-includes/rest-api.php | Gets the REST API route for a term. |
| WP_REST_Posts_Controller::prepare_links() wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares links for the request. |
| get_term_field() wp-includes/taxonomy.php | Gets sanitized term field. |
| get_post_type() wp-includes/post.php | Retrieves the post type of the current post or of a given post. |
| rest_url() wp-includes/rest-api.php | Retrieves the URL to a REST endpoint. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Menu_Items_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php | Prepares a single post output for response. |
Changelog
| Version | Description |
|---|---|
| 5.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_menu_items_controller/prepare_links