预计阅读时间: 2 分钟

使用模版函数需要修改模版,ACF也提供了shortcode方式,不需修改模版依然能使用其数据。

模版函数
显示一个字段
field_name代表字段名称,指创建custom field时“Field Name”中填写的内容,只能输入字母和下划线。


<p><?php the_field('field_name'); ?></p>

the_field()的使用类似WordPress的the_title(),直接输出内容。

返回字段的值

the_field()会直接显示字段的值,要返回的话,就要用get的方式


<?php
$variable = get_field('field_name');
 // 现在可以使用$variable了
?>

使用前判断值是否存在

如果(value == “” || value == null || value == false),则get_field()返回false,这样就可以先判断值是否存在,再输出


<?php
if(get_field('field_name'))
{
echo '<p>' . get_field('field_name') . '</p>';
}
?>

处理数组类型的返回值

ACF中image、file等类型可以用数组形式(如选择image object)返回更多的数据,而不仅仅是只返回image url或者file url。

下面的代码演示先用get_field()获取数组,在用foreach循环读取数组元素的方法。


<?php
 
$values = get_field('field_name');
if($values)
{
echo '<ul>';
 
foreach($values as $value)
{
echo '<li>' . $value . '</li>';
}
 
echo '</ul>';
}
 
// 用var_dump()看一看$values的真面目吧
var_dump($values);
 
?>

处理返回url的image类型

如果创建image type时选择返回url,代表返回值是一个字符串,所以显示方法如下


<img src="<?php the_field('image_test'); ?>" alt="" />

处理返回ID的image类型

如果创建image type时选择返回image ID,则返回值会是一个整数,我们当然不想显示整数,要根据这个ID获取更多信息。


<?php $image = wp_get_attachment_image_src(get_field('image_test'), 'full'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('image_test')) ?>" />

获取Repeater Field的值

Repeater Field是ACF的付费组件,repeater field的值可以由 get_field、 the_repeater_field /the_sub_field获取


<?php
 
// 方法一:直接使用数组方式操作,$rows是一个数组
 
$rows = get_field('repeater_field_name');
if($rows)
{
echo '<ul>';
 
foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
}
 
echo '</ul>';
}
 
// 方法二:使用has_sub_field + get_sub_field的组合获取
 
if(get_field('repeater_field_name'))
{
echo '<ul>';
 
while(has_sub_field('repeater_field_name'))
{
echo '<li>sub_field_1 = ' . get_sub_field('sub_field_1') . ', sub_field_2 = ' . get_sub_field('sub_field_2') .', etc</li>';
}
 
echo '</ul>';
}
 
//方法三:使用the_repeater_field + the_sub_field的组合获取
if(get_field('repeater_field_name')): ?>
 
<ul>
 
<?php while(has_sub_field('repeater_field_name')): ?>
 
<li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
 
<?php endwhile; ?>
 
</ul>
 
<?php endif; ?>

随机获取repeater fleid中的一个元素


<?php
 
$rows = get_field('repeater_field_name');
$row_count = count($rows);
$i = rand(0, $row_count - 1);
 
echo $rows[$i]['sub_field_name'];
 
?>

从非当前页面获取数据


<?php
//要获取数据的页面的post ID
$other_page = 12;
 
//直接显示内容
?>
<p><?php the_field('field_name', $other_page); ?></p>
 
<?php
//不直接显示,先将值赋给一个变量
// get variable
$variable = get_field('field_name', $other_page);
 
// 以repeater类型为例 -注意 the_sub_field和get_sub_field函数不需要post ID参数
if(get_field('repeater_field_name', $other_page))
{
echo '<ul>';
 
while(has_sub_field('repeater_field_name', $other_page))
{
echo '<li>sub_field_1 = ' . get_sub_field('sub_field_1') . ', sub_field_2 = ' . get_sub_field('sub_field_2') .', etc</li>';
}
 
echo '</ul>';
}
 
?>

使用ACF的值查询数据

本质是根据custom field的值查询post。


<?php
 
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location', //acf中定义
'meta_value' => 'melbourne'
));
 
if($posts)
{
echo '<ul>';
 
foreach($posts as $post)
{
echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
}
 
echo '</ul>';
}
 
?>

Shortcode方式

[acf field="field_name"]
使用这个shortcode,就可以直接在所见即所得编辑器中使用ACF的值,不过有一些局限性:

只能获取当前文章的ACF内容
对repeater field不适用
使用例子


This is a story about a boy named [acf field="name"]. He is [acf field="age"] years old.

参考文档
ACF Code Examples
ACF Shortcodes

此文章对你有帮助吗? 已有 0 人说这篇文章是有用的。