Last active
July 5, 2017 14:04
-
-
Save claudioweb/c5e309c993079eb477a16cce0e6d5a05 to your computer and use it in GitHub Desktop.
Retorno em Json Wordpress Ajax
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 | |
//Nome da Action | |
$action_name = 'posts'; | |
// Definindo action ajax | |
add_action('wp_ajax_'.$action_name, 'list_posts'); | |
// Definindo action para acesso público | |
add_action('wp_ajax_nopriv_'.$action_name, 'list_posts'); | |
//Função que irá retornar o JSON | |
function list_posts(){ | |
// Definindo o retorno como JSON | |
header( "Content-type: application/json"); | |
$args = array( | |
'post_type' => 'post', | |
'posts_per_page' => -1 | |
); | |
$posts = get_posts($args); | |
die(json_encode($posts)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment