我目前有一个名为newcpt的自定义帖子类型.这有2个分类,区域和城市.
在区域我有regiona和regionb,在城市我有citya和cityb.
在区域我有regiona和regionb,在城市我有citya和cityb.
我目前正在使用wp_dropdown_categories来获取regiona的下拉列表.有没有一个快速或简单的解决方案来显示第二个下拉列表,只有当它还有regiona作为分类时才会显示.
所以举个例子.我有一个testpost的帖子,在regiona和citya,当从下拉列表中选择regiona时,我想第二次下拉到这个地区的城市,所以在这种情况下citya
我认为最好的解决方案是使用
PHP生成数据集并使用例如AngularJS在客户端站点上显示.
原文链接:https://www.f2er.com/php/134087.htmlangular .module('app',[]) .controller('controller',[ '$scope','$attrs',function ($scope,$attrs) { $scope.data=JSON.parse($attrs.selects); $scope.region = {}; $scope.city = {}; $scope.loadCitys = function () { $scope.region = JSON.parse($scope.region); }; $scope.loadPosts = function () { $scope.city = JSON.parse($scope.city); }; } ]);
和HTML
<!DOCTYPE html> <html ng-app="app"> <head> <script src="https://code.angularjs.org/1.5.8/angular.js"></script> <link rel="stylesheet" href="style.css" /> </head> <body ng-controller="controller" data-selects='[{"name":"Region 1","citys":[{"name":"City 1","posts":[{"title":"Post 1","id":1},{"title":"Post 2","id":2}]}]},{"name":"Region 2","citys":[{"name":"City 2","posts":[{"title":"Post 3","id":3},{"title":"Post 4","id":4}]},{"name":"City 3","posts":[{"title":"Post 5","id":5},{"title":"Post 6","id":6}]}]}]'> <div> Region: {{region.name}} <br> City: {{city.name}} </div> <select ng-model="region" ng-change="loadCitys()"> <option ng-repeat="tregion in data" value="{{tregion}}">{{tregion.name}}</option> </select> <select ng-model="city" ng-change="loadPosts()"> <option ng-repeat="tcity in region.citys" value="{{tcity}}">{{tcity.name}}</option> </select> <select ng-model="post"> <option ng-repeat="tpost in city.posts" value="{{tpost}}">{{tpost.title}}</option> </select> <script src="script.js"></script> </body> </html>
活例:https://plnkr.co/edit/gyzCxMpn9luAcGsLZHYD
(你必须敲定它)