首页 文章

高级自定义字段 - Wordpress

提问于
浏览
3

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

我创建了一个名为 book_cover_thumbnail 的字段组,其中有一个链接到它 . 任何人都可以看到为什么下面的代码不起作用?

<img src="<?php get_field('book_cover_thumbnail');?>" />

我没有任何错误,没有空格 .

2 回答

  • 4

    确保你是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; ?>`
    

    或者您将post id添加到 get_field() 函数作为参数:

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

    Documentation:

    使用 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/

  • 3

    将get_field更改为the_field . 获取字段返回值但不回显它 .

    或者,在get字段前放置一个echo .

相关问题