tombroucke / acf-objects
使 ACF 的 get_field 方法能够返回带有易用方法的对象
3.10.0
2023-12-20 13:23 UTC
Requires
- php: >=8
Requires (Dev)
- illuminate/support: ^9.36
- php-stubs/acf-pro-stubs: ^6.0
- phpunit/phpunit: ^9.5
- roave/security-advisories: dev-latest
- squizlabs/php_codesniffer: ^3.6.2
- szepeviktor/phpstan-wordpress: ^1.1
This package is auto-updated.
Last update: 2024-09-20 14:51:29 UTC
README
安装
composer require tombroucke/acf-objects
使用
- 在 sage 10 中,Acorn 应该能够找到 src/AcfObjectsServiceProvider.php。如果找不到,你应在 /config/app.php 中的 providers 数组中添加此提供者
Otomaties\AcfObjects\AcfObjectsServiceProvider::class
- 要使用此库而不使用 Acorn,请使用以下片段
add_filter('acf/format_value', function ($value, $post_id, $field) { $value = \Otomaties\AcfObjects\Acf::findClassByFieldType($value, $post_id, $field); return $value; }, 99, 3);
示例
在您想要使用 ACF 对象的任何地方,导入 Otomaties\AcfObjects\Acf 类
<?php use Otomaties\AcfObjects\Acf; ?>
测试字段是否有值
<?php if(Acf::getField('fieldname')->isSet()): ?> <?php endif; ?>
具有数组访问权限的字段(图库、重复器、灵活内容)
<?php if(!Acf::getField('fieldname')->isEmpy()): ?> <?php endif; ?>
日期选择器
<?php echo Acf::getField('date')->format('d/m/Y'); ?>
文件
<?php echo Acf::getField('file')->url(); ?> <?php echo Acf::getField('file')->title(); ?> <?php echo Acf::getField('file')->filesize(); ?>
图库
<ul> <?php foreach (Acf::getField('gallery') as $image): ?> <li><?php echo $image->attributes(['class' => 'd-none'])->image(); ?></li> <?php endforeach; ?> </ul>
图库数组访问
<?php echo Acf::getField('gallery')[1]->image(); ?>
谷歌地图
<?php echo Acf::getField('google_map')->address(); ?> <?php echo Acf::getField('google_map')->lat(); ?> <?php echo Acf::getField('google_map')->lat(); ?> ...
分组
<?php echo Acf::getField('group')->get('text'); ?>
图片
<?php echo Acf::getField('image')->url('medium'); ?>
图片标签
<?php echo Acf::getField('image')->image('medium'); ?>
具有属性的图片标签,用链接包裹
<a href="<?php echo Acf::getField('image')->url('full'); ?>"> <?php echo Acf::getField('image')->attributes(['class' => 'w-100'])->image('thumbnail'); ?> </a>
使用媒体库中的默认图片,媒体 ID 为 48 的图片
<?php echo Acf::getField('image')->default(48, 'thumbnail')->image('thumbnail'); ?>
使用 URL 的默认图片的图片
<?php echo Acf::getField('image')->default('https://picsum.photos/150/150')->image('thumbnail'); ?>
链接
<?php echo Acf::getField('link')->link(); // Will output an a-tag with href, target & title ?>
<?php echo Acf::getField('link')->attributes(['class' => 'btn btn-primary','data-foo' => 'bar'])->link(); ?>
<a href="<?php echo Acf::getField('link')->url(); ?>" target="<?php echo Acf::getField('link')->target(); ?>"> <?php echo Acf::getField('link')->title(); ?> </a>
重复器
<table class="table"> <?php foreach (Acf::getField('repeater') as $key => $row): ?> <tr> <td><?php echo $key; ?></td> <td><?php echo $row->get('text'); ?></td> <td><?php echo $row->get('image')->image('thumbnail'); ?></td> </tr> <?php endforeach; ?> </table>
重复器数组访问
<?php if (isset(Acf::getField('repeater')[0])): ?> <?php echo Acf::getField('repeater')[0]->get('text'); ?> <?php endif; ?>
文本
<?php echo Acf::getField('text'); ?>
<?php echo Acf::getField('text')->default('Default text'); ?>
来自其他文章的文本
<?php echo Acf::getField('text', 51); ?>
ACF Fluent
在开发此包的过程中,我偶然发现了 samrap 的 ACF Fluent,它工作得很好。ACF Fluent 和此包之间最大的区别是轻松显示图像的能力: Acf::getField('image')->image('thumbnail')
以及更直观地遍历重复器和图库字段。