Ric*_*ich 11 javascript php wordpress json angularjs
我正在使用angularjs来加载我的wordpress帖子列表,但是我无法使用我的部分文件处理我的任何php函数.
我尝试过使用像search.php而不是search.html这样的东西,但是当这样做时我得到了致命错误等错误,get_post_meta是未定义的.
现在我知道我们不应该将客户端与服务器端混合,我可以使用某种服务来解析我的php,但我不知道如何去做.我需要我的search.php来渲染我的php标签,这样我就可以显示自定义字段,并使用我在那里的几个php函数.
这是最好的方法吗?
在我的页面模板(.php)上,我有 -
<div id="page" ng-app="app">
<header>
<h1>
<a href="<?php echo home_url(); ?>">Search</a>
</h1>
</header>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div ng-Cloak ng-controller="MyController" class="my-controller">
<div ng-view></div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php rewind_posts(); ?>
<div ng-controller="OtherController" class="other-controller">
<div class="text-center">
<dir-pagination-controls boundary-links="true" on-page-change="pageChangeHandler(newPageNumber)" template-url="/partials/dirPagination.tpl.html"></dir-pagination-controls>
</div>
</div>
<footer>
© <?php echo date( 'Y' ); ?>
</footer>
</div>
Run Code Online (Sandbox Code Playgroud)
在我的php文件中我希望被调用到视图中具有如下功能 -
<?php
$pcomp1b = get_post_meta(get_the_ID(), 'pa_meta_comp1b', true);
$pcomp1c = get_post_meta(get_the_ID(), 'pa_meta_comp1c', true);
$pcomp1d = get_post_meta(get_the_ID(), 'pa_meta_comp1d', true); ?>
Run Code Online (Sandbox Code Playgroud)
数学 -
if( is_numeric( $price1 ) ) {
$a1 = $price1;
}
$b1 = $pcomp1d;
$sqft1 = str_replace( ',', '', $b1 );
if( is_numeric( $sqft1 ) ) {
$b1 = $sqft1;
}
$a2 = $pcomp2f;
$price2 = str_replace( ',', '', $a2 );
if( is_numeric( $price2 ) ) {
$a2 = $price2;
}
$b2 = $pcomp2d;
$sqft2 = str_replace( ',', '', $b2 );
if( is_numeric( $sqft2 ) ) {
$b2 = $sqft2;
}
$a3 = $pcomp3f;
$price3 = str_replace( ',', '', $a3 );
if( is_numeric( $price3 ) ) {
$a3 = $price3;
}
$b3 = $pcomp3d;
$sqft3 = str_replace( ',', '', $b3 );
if( is_numeric( $sqft3 ) ) {
$b3 = $sqft3;
}
$ppsqft1 = ROUND($price1 / $sqft1);
$ppsqft2 = ROUND($price2 / $sqft2);
$ppsqft3 = ROUND($price3 / $sqft3);
$ppsav = ROUND((($ppsqft1 + $ppsqft2 + $ppsqft3)/3));
$b4 = $property_area;
$parea = str_replace( ',', '', $b4 );
if( is_numeric( $parea ) ) {
$b4 = $parea;
}
$ehvp = $ppsav * $parea;
$homevalue = number_format($ehvp, 0, '.', ',');
echo '$' . $homevalue; ?>
Run Code Online (Sandbox Code Playgroud)
和功能 -
<?php if (class_exists('MRP_Multi_Rating_API')){ MRP_Multi_Rating_API::display_rating_result(array('rating_item_ids' => 2, 'show_count' => false, 'result_type' => 'value_rt', 'no_rating_results_text' => 'N/A'));} ?>
Run Code Online (Sandbox Code Playgroud)
我的应用脚本 -
var app = angular.module('app', ['ngRoute', 'ngSanitize', 'angularUtils.directives.dirPagination'])
function MyController($scope) {
$scope.currentPage = 1;
$scope.pageSize = 2;
$scope.posts = [];
$scope.pageChangeHandler = function(num) {
console.log('search page changed to ' + num);
};
}
function OtherController($scope) {
$scope.pageChangeHandler = function(num) {
console.log('going to page ' + num);
};
}
app.config(function(paginationTemplateProvider) {
paginationTemplateProvider.setPath('/partials/dirPagination.tpl.html');
});
app.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when('/search-results', {
templateUrl: myLocalized.partials + 'main.html',
controller: 'Main'
})
.when('/:ID', {
templateUrl: myLocalized.partials + 'content.html',
controller: 'Content'
});
})
app.controller('Main', function($scope, $http, $routeParams) {
$http.get('wp-json/posts?type=property').success(function(res){
$scope.posts = res;
});
})
app.controller('Content', function($scope, $http, $routeParams) {
$http.get('wp-json/posts?type=property/?filter["posts_per_page"]=25&filter["orderby"]=date&filter["order"]=desc/' + $routeParams.ID).success(function(res){
$scope.post = res;
});
});
app.controller('MyController', MyController);
app.controller('OtherController', OtherController);
Run Code Online (Sandbox Code Playgroud)
那么我怎样才能使用ng-view和partials模板呢?
UPDATE
我确实使用wordpress api,我知道{{tag}} ...我需要用PHP完成特定的事情.有没有办法将其预处理到部分文件中?
如果你想在外部php文件(不是默认模板文件)中包含wordpress本机函数,你可以通过加载wp-blog-header.php或加载wp-load.php到该文件来加载wordpress默认函数
喜欢require_once("/path/to/wordpress/wp-load.php");在一开始就添加.
你可以参考下面的代码来获取$ http请求检索json数据,
var app = angular.module('recentPost', []);
app.controller('PostController', ['$scope', '$http', function($scope, $http) {
$http.get('your_json_url')
.then(function(response) {
console.log( response );
/*if ( response.data !== '-1' ) {
$scope.lists= JSON.parse(response.data.data_received);
} else {
$scope.lists= response;
}*/
}
}]);
Run Code Online (Sandbox Code Playgroud)
另外,我没有看到将AngularJS直接用于wordpress的问题,你已经可以使用jQuery + Ajax,没有必要加载额外的库
您可以使用 php 文件来处理所有与查询相关的内容,并使用某种 ajax 功能调用该 php 文件,并将响应数据绑定到 Angular 中的某些 div .. 请查找下面的示例:
<script type="text/javascript">
var sampleApp = angular.module('sampleApp', []); // Define new module for our application
// Create new controller, that accepts two services $scope and $http
function SampleCtrl($scope, $http) {
$scope.date = "sample date"; // Bind data to $scope
// Define new function in scope
$scope.fetch = function() {
// Use $http service to fetch data from the PHP web service
$http.get('api.php').success(function(data) {
$scope.date = data.date; // Bind the data returned from web service to $scope
});
}
};
SampleCtrl.$inject = ['$scope', '$http']; // Ask Angular.js to inject the requested services
sampleApp.controller('SampleCtrl', SampleCtrl); // Initialize controller in pre-defined module
</script>
Run Code Online (Sandbox Code Playgroud)
他们已将所有 php 代码放入 api.php 文件中,并将响应数据绑定到 $scope 变量
现在,当按下下面的按钮时,他们将从 php 文件中获取数据并将其放入 HTML 文件中
<body ng-controller="SampleCtrl">
<div>
<!-- When button is clicked the controller will use the fetch() in $scope
<button ng:click="fetch()">Load date</button>
<hr>
<!-- {{date}} is bound to $scope.date and automatically updates when changed ($scope.fetch()) call -->
<h1>Date is {{date}}!</h1>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
您可以使用此代码作为参考
| 归档时间: |
|
| 查看次数: |
1218 次 |
| 最近记录: |