Last active
October 31, 2018 13:44
-
-
Save dbranes/83a8f93597cc0874fc48 to your computer and use it in GitHub Desktop.
Return ACF variable from wp-json (rest api & angular) @see http://www.wpquestions.com/question/showChrono/id/10627
This file contains hidden or 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
/** | |
* Add ACF data to the WP JSON REST API | |
*/ | |
add_filter( 'json_prepare_post', 'wpq_add_acf' ); | |
function wpq_add_acf( $post ) { | |
if( function_exists( 'get_fields' ) ) | |
{ | |
if ( is_array( $post ) ) | |
$post['acf'] = get_fields( $post['ID'] ); | |
} | |
return $post; | |
} |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.4/angular.min.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.4/angular-route.js"></script> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.0-beta.4/angular-sanitize.js"></script> | |
<base href="/" /> | |
<script> | |
var app = angular.module("roots", ['ngRoute', 'ngSanitize'] ); | |
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { | |
$locationProvider.html5Mode(true); | |
$routeProvider | |
// get acf home | |
.when('/', { | |
templateUrl: 'homeacf.html', | |
controller: 'HomeACF' | |
}) | |
//end acf home | |
.otherwise({ | |
redirectTo: '/' | |
}); | |
}]); | |
app.controller("HomeACF", function($scope, $http) { | |
$http.get('http://example.tld/wp-json/posts/4'). | |
success(function(data, status, headers, config) { | |
$scope.singlepost = data; | |
}). | |
error(function(data, status, headers, config) { | |
// log error | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div ng-app="roots"> | |
<div ng-controller="HomeACF"> | |
<p>Title: {{singlepost.title}} </p> | |
<p>ACF: {{singlepost.acf.image_stock.ID}} </p> | |
<p>Meta: {{singlepost.meta.links.self}} </p> | |
</div> | |
<div ng-view></div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment