matgargano / acf-post-type-selector
Advanced Custom Fields 的帖子类型选择器。
1.0.1
2022-08-09 20:35 UTC
This package is auto-updated.
Last update: 2024-09-10 01:18:57 UTC
README
- 贡献者: @timperry, @shaunbent, @emaildano, @rafegoldberg, @thomasdebruin, @gnowland
- 标签: ACF, Post Type Selector
- 至少需要 4.0
- 测试到 5.0.0
- 稳定标签: trunk
- 许可证: GPLv2 或更高版本
- 许可证 URI: https://gnu.ac.cn/licenses/gpl-2.0.html
描述
提供选择单个或多个帖子类型的选项
兼容性
此附加组件将与以下版本一起工作
- 版本 5 及以上
- 版本 4 及以上
安装
此附加组件既可以被视为 WP 插件,也可以被视为主题包含文件。
插件
- 将 'acf-post-type-selector' 文件夹复制到您的插件文件夹中
- 通过插件管理页面激活插件
包含
- 将 'acf-post-type-selector' 文件夹复制到您的主题文件夹中(可以使用子文件夹)。您可以在 'wp-content' 目录的任何位置放置文件夹
- 编辑您的 functions.php 文件并添加以下代码(确保路径正确以包含 acf-post-type-selector.php 文件)
ACF 版本 4
add_action( 'acf/register_fields', 'my_register_fields' );
function my_register_fields() {
include_once( 'acf-post-type-selector/post-type-selector-v4.php' );
}
ACF 版本 5
add_action( 'acf/include_fields', 'my_register_fields' );
function my_register_fields() {
include_once( 'acf-post-type-selector/post-type-selector-v5.php' );
}
用法
获取字段值
$post_type_var = 'post_type';
$post_type = get_sub_field( $post_type_var );
获取字段值并显示标签
<?php
// get post type fields
$post_type_var = 'post_type';
$post_type = get_sub_field( $post_type_var );
// get post type label
$post_type_object = get_post_type_object( $post_type ); ?>
<li><a href="<?php echo get_post_type_archive_link( $post_type ); ?>"><?php echo $post_type_object->label; ?></a></li>
过滤可用的帖子类型
默认情况下,仅显示具有参数 public
的帖子类型。如果您需要显示非公开帖子类型,请使用以下描述的过滤器 post_type_selector_post_types
。此过滤器还允许您从列表中删除帖子类型。
add_filter( 'post_type_selector_post_types', function( $post_types, $field ) {
$post_types['foo'] = get_post_type_object( 'foo' );
unset( $post_types['post'] );
return $post_types;
}, 10, 2 );
变更日志
1.0.1
- 过滤可用的帖子类型
1.0.0
- 支持 ACF
v5
0.0.1
- 首次发布。