Skip to content

Instantly share code, notes, and snippets.

@yoren
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoren/c6f88b50950b368b4a34 to your computer and use it in GitHub Desktop.
Save yoren/c6f88b50950b368b4a34 to your computer and use it in GitHub Desktop.
Category Pages In AngularJS WordPress Theme
<search-form></search-form>
<p>Categories:</p>
<ul>
<li ng-repeat="category in categories">
<span ng-if="current_category_id && category.ID == current_category_id" ng-bind-html="category.name"></span>
<a ng-if="!current_category_id || category.ID != current_category_id" href="category/{{category.ID}}" ng-bind-html="category.name"></a>
</li>
</ul>
<p>{{pageTitle}}</p>
<!-- ... -->
<search-form></search-form>
<p>Categories:</p>
<ul>
<li ng-repeat="category in categories">
<span ng-if="current_category_id && category.ID == current_category_id" ng-bind-html="category.name"></span>
<a ng-if="!current_category_id || category.ID != current_category_id" href="category/{{category.slug}}" ng-bind-html="category.name"></a>
</li>
</ul>
<!-- ... -->
<search-form></search-form>
<p>Categories:</p>
<ul>
<li ng-repeat="category in categories">
<a href="category/{{category.ID}}" ng-bind-html="category.name"></a>
</li>
</ul>
<p>{{pageTitle}}</p>
<!-- .... -->
//...
//Category controller
app.controller('Category', ['$scope', '$routeParams', '$http', function($scope, $routeParams, $http) {
$http.get('wp-json/taxonomies/category/terms').success(function(res){
$scope.categories = res;
});
$http.get('wp-json/taxonomies/category/terms/?filter[slug]=' + $routeParams.category).success(function(res){
$scope.current_category_id = res[0].ID;
$scope.pageTitle = 'Posts in ' + res[0].name + ':';
document.querySelector('title').innerHTML = 'Category: ' + res[0].name + ' | AngularJS Demo Theme';
$http.get('wp-json/posts/?filter[category_name]=' + res[0].name).success(function(res){
$scope.posts = res;
});
});
}]);
//...
//...
//Category controller
app.controller('Category', ['$scope', '$routeParams', '$http', function($scope, $routeParams, $http) {
$http.get('wp-json/taxonomies/category/terms').success(function(res){
$scope.categories = res;
});
$http.get('wp-json/taxonomies/category/terms/' + $routeParams.category).success(function(res){
$scope.current_category_id = $routeParams.category;
$scope.pageTitle = 'Posts in ' + res.name + ':';
document.querySelector('title').innerHTML = 'Category: ' + res.name + ' | AngularJS Demo Theme';
$http.get('wp-json/posts/?filter[category_name]=' + res.name).success(function(res){
$scope.posts = res;
});
});
}]);
//...
var app = angular.module('app', ['ngRoute', 'ngSanitize']);
//Config the route
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
templateUrl: myLocalized.partials + 'main.html',
controller: 'Main'
})
.when('/blog/:ID', {
templateUrl: myLocalized.partials + 'content.html',
controller: 'Content'
})
.when('/category/:category', {
templateUrl: myLocalized.partials + 'main.html',
controller: 'Category'
})
.otherwise({
redirectTo: '/'
});
}]);
//...
var app = angular.module('app', ['ngRoute', 'ngSanitize']);
//...
//Main controller
app.controller('Main', ['$scope', '$http', function($scope, $http) {
$http.get('wp-json/taxonomies/category/terms').success(function(res){
$scope.categories = res;
});
$http.get('wp-json/posts/').success(function(res){
$scope.posts = res;
$scope.pageTitle = 'Latest Posts:';
document.querySelector('title').innerHTML = 'Home | AngularJS Demo Theme';
});
}]);
//...
@yoren
Copy link
Author

yoren commented Apr 11, 2015

Please refer to the tutorial on 1Fix.io: Category Pages in AngularJS WordPress Theme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment