首页 WordPress函数大全 the_field()

the_field()

2020-06-28 / 1468阅 / 悠然

如何你看完本文还不知道如何使用该函数,可以联系我定制视频教程,50元/个函数,学不会全额退款

本视频需要登录以后才能播放

登录

描述

显示特定字段的值。

此功能直观而强大,可用于从任何位置输出任何字段的值。请注意,此功能与相同echo get_field()

参量

the_field($selector, [$post_id], [$format_value]); 
  • $selector (字符串) (必填) 字段名称或字段键。
  • $post_id (混合) (可选) 保存值的帖子ID。默认为当前帖子。
  • $format_value (布尔) (可选) 是否应用格式化逻辑。默认为true。

例子

显示当前帖子中的值

本示例说明如何显示当前帖子中字段“ text_field”的值。

<h2><?php the_field('text_field'); ?></h2> 

显示特定帖子中的值

本示例说明如何显示ID = 123的帖子中字段“ text_field”的值。

<h2><?php the_field('text_field', 123); ?></h2> 

检查值是否存在

此示例显示如何在显示值之前检查值是否存在。

<?php if( get_field('text_field') ): ?>
    <h2><?php the_field('text_field'); ?></h2>
<?php endif; ?> 

从不同的对象获取价值

本示例显示了各种有效的$ post_id值,这些值指定将值保存在何处。

$post_id = false;           // current post
$post_id = 123;             // post ID = 123
$post_id = "user_123";      // user ID = 123
$post_id = "term_123";      // term ID = 123
$post_id = "category_123";  // same as above
$post_id = "option";        // options page
$post_id = "options";       // same as above

the_field( 'my_field', $post_id ); 
大家谈论
    我的见解