高级自定义字段 - Wordpress

2ne*_*2ne 3 php wordpress advanced-custom-fields

使用自定义字段插件时,我无法让它返回任何数据。

我创建了一个名为的字段组book_cover_thumbnail,其中有一个帖子链接到它。谁能明白为什么下面的代码不起作用?

<img src="<?php get_field('book_cover_thumbnail');?>" />
Run Code Online (Sandbox Code Playgroud)

我完全没有收到任何错误,没有空格。

Nat*_* F. 5

确保您 a) 使用 或 回显字段the_field(),echo get_field()并且 b) 此代码位于 wordpress 循环内,如下所示:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>`
<img src="<?php echo get_field('book_cover_thumbnail');?>" />
<?php end while; endif; ?>`
Run Code Online (Sandbox Code Playgroud)

或者将帖子 IDget_field()作为参数添加到函数中:

$post_id = *your_post_ID_here*;
<img src="<?php echo get_field('book_cover_thumbnail', $post_id);?>" />
Run Code Online (Sandbox Code Playgroud)

文档:

使用get_field():http ://www.advancedcustomfields.com/resources/functions/get_field/

使用the_field():http ://www.advancedcustomfields.com/resources/functions/the_field/

所有高级自定义字段文档:http://www.advancedcustomfields.com/resources/