Last active
June 18, 2019 23:18
-
-
Save ribeirovictor/b3e89471320327f3cf301c067623aa96 to your computer and use it in GitHub Desktop.
[Pagcerto] Rota customizada Wordpress Rest API para mostrar as 3 primeiras postagens do blog (título, link e imagem destacada)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// API Posts | |
function get_post_items() { | |
$args = array ( | |
'post_status' => 'publish', | |
'posts_per_page' => 3, | |
); | |
$items = array(); | |
if ( $posts = get_posts( $args ) ) { | |
foreach ( $posts as $post ) { | |
$items[] = array( | |
'id' => $post->ID, | |
'link' => get_permalink($post->ID), | |
'title' => $post->post_title, | |
'image' => wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "medium"), | |
'teaser' => $post->post_excerpt | |
); | |
} | |
} | |
return $items; | |
} | |
function register_api_endpoints() { | |
register_rest_route( 'api/v1', '/posts', array( | |
'methods' => 'GET', | |
'callback' => 'get_post_items', | |
) ); | |
} | |
add_action( 'rest_api_init', 'register_api_endpoints' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment