moxie-leean / wp-endpoints-post
Requires
- php: >=5.4
- moxie-leean/wp-acf: 0.*.*
- moxie-leean/wp-endpoint: 0.*.*
- moxie-leean/wp-utils: 0.*.*
Requires (Dev)
This package is auto-updated.
Last update: 2022-02-01 12:56:25 UTC
README
通用的可自定义视图端点,用于通过WP-API公开内容。此扩展将创建一个端点(默认为
`
/wp-json/leean/v1/view`
),返回前端应用程序渲染视图所需的所有数据。
端点接受一个slug参数(例如`
/wp-json/leean/v1/view?slug=sample-page`
)并返回以下数据
- Id
- 模板
- 内容
- WordPress默认字段:标题,内容
- 与页面关联的所有ACF字段,按其ACF字段组分组。
- 元数据:标题,描述,开放图(注意这尚未实现)。
注意:目前它仅适用于单个帖子(包括页面和cpt)。我们需要考虑其他类型视图的最佳解决方案,例如存档页面。
入门
安装此包的最简单方法是使用终端中的composer
composer require moxie-leean/wp-endpoints-view --save
或者在你的composer.json
文件中添加以下行
"require": {
"moxie-leean/wp-endpoints-view": "dev-master"
}
这将从packagist网站下载文件,并从仓库的master分支设置最新的版本。
之后,你可以包含autoload.php
文件,以便在对象创建期间自动加载类。
include '/vendor/autoload.php';
最后,你需要初始化端点,在你的代码中添加以下内容
\Leean\Endpoints\View::init();
用法
此扩展具有多个过滤器,可用于自定义输出。此外,它还进行了一些有用的ACF数据操作,使它对前端应用程序更有用。
过滤器
许多过滤器传递的常见参数
- $endpoint : 端点名称。对于此扩展始终是'/view'。
- $post_id : 帖子ID。
- $field : ACF 字段对象。
ln_endpoints_api_namespace
自定义API命名空间(在`
/wp-json/leean/v1/view`
中的'leean')
add_filter( 'ln_endpoints_api_namespace', function( $namespace, $endpoint ) {
return 'my-app';
}, 10, 2 );
ln_endpoints_api_version
自定义API版本(在`
/wp-json/leean/v1/view`
中的'v1')
add_filter( 'ln_endpoints_api_version', function( $version, $endpoint ) {
return 'v2';
}, 10, 2 );
ln_endpoints_query_args
自定义在查询帖子之前使用WP_Query的查询参数。$request是端点处理程序接收到的WP_REST_Request对象。
add_filter( 'ln_endpoints_{endpoint}_query_args', function( $query_args, $request ) {
$query_args['post_type'] = 'page';
return $query_args;
}, 10, 3 );
ln_endpoints_data
在发送之前自定义结果。
add_filter( 'ln_endpoints_data_{endpoint}', function( $data, $post_id ) {
$data['content']['title'] = '***' . $data['content']['title'] . '***';
return $data;
}, 10, 3 );
在前两个过滤器中,{endpoint}
是端点名称,在这种情况下是post
,因此例如ln_endpoints_data_post
是你应该使用的过滤器名称来过滤数据。
ln_endpoints_acf
自定义ACF字段的值。
add_filter( 'ln_endpoints_acf', function( $value, $endpoint, $post_id, $field ) {
if ( 'image' === $field['type'] ) {
return 'https://upload.wikimedia.org/wikipedia/commons/1/1b/Nice-night-view-with-blurred-cars_1200x900.jpg';
}
return $value;
}, 10, 4 );
ln_endpoints_acf_image_size
设置要使用的图像大小。仅针对返回格式设置为'image id'的图像字段激活。注意,$sub_field仅用于重复项中的图像。
add_filter( 'ln_endpoints_acf_image_size', function( $size, $endpoint, $post_id, $field, $sub_field ) {
if ( 'logo' === $field['name'] ) {
return 'very_small';
}
return $size;
}, 10, 4 );
ln_endpoints_acf_repeater_as_array
是否以数组的形式返回重复项。仅针对具有恰好一个值的重复项字段激活。
add_filter( 'ln_endpoints_acf_repeater_as_array', function( $as_array, $endpoint, $post_id, $field ) {
$post = get_post( $post_id );
return 'training' === $post->page_template && 'cta' === $field['name'] ? false : $as_array;
}, 10, 4 );
ACF操作
帖子
当ACF字段类型为'Post Object'且返回格式为'id'时激活。获取帖子的所有内容(包括其ACF字段)。如果设置了'选择多个值'选项,则返回帖子数据数组。
图像
当ACF字段类型为'Image'且返回格式为'id'时激活。图像大小必须使用`
ln_endpoints_acf_image_size`
过滤器设置。返回图像URL、宽度、高度和alt。
重复器
当ACF字段类型为'Repeater'且恰好有一个项目时激活。它通过一个过滤器`
ln_endpoints_acf_repeater_as_array`
,该过滤器在为false时返回一个对象而不是数组。